Reputation: 281
I am trying to make an application that allows the user to search through the College Scorecard API. I am very new to ajax so I am not sure what I have done incorrectly here. I have the textAjax() function hooked to a button on my HTML form, but when I run my code, the request fails. Here is my code.
function testAjax(){
$.ajax({
type : 'POST',
url : 'https://api.data.gov/ed/collegescorecard/v1/schools?school.name=University%20of%20Cincinnati&api_key=<API-KEY>',
dataType : 'json',
success : function(data) {
//if the request is successful do something with the data
alert(data);
},
error : function(request){
//if the request fails, log what happened
alert(JSON.stringify("Error: " + request));
}
});
}
function buttonClick() {
var url = testAjax();
}
Upvotes: 2
Views: 1186
Reputation: 1022
There is a data
attribute for queries to be added to your url so you don't have to do it yourself. Check this out:
function testAjax(){
$.ajax({
type : 'POST',
url : 'https://api.data.gov/ed/collegescorecard/v1/schools',
data: {
'name': 'University of Cincinnati',
'api_key': 'whatever'
},
dataType : 'json',
success : function(data) {
//if the request is successful do something with the data
alert(data);
},
error : function(request){
//if the request fails, log what happened
alert(JSON.stringify("Error: " + request));
}
});
I'm not sure if this is your only problem, but it might help.
Upvotes: 2