【发布时间】:2016-12-01 06:00:57
【问题描述】:
我正在尝试制作一个允许用户通过 College Scorecard API 进行搜索的应用程序。我对 ajax 很陌生,所以我不确定我在这里做错了什么。我将 textAjax() 函数连接到我的 HTML 表单上的一个按钮,但是当我运行我的代码时,请求失败。这是我的代码。
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();
}
【问题讨论】:
-
请求如何失败?网络错误?如果是这样,请与我们分享来自 API 的响应
-
你在做
POST,而你应该做GET? - 看起来应该是GET(作为搜索和所有)
标签: javascript jquery