我正在尝试将 Instapaper Simple API 集成到某些东西中,但我很难理解如何处理 API 在 Javascript 中发回的响应。这篇文章很好地添加到了 Instapaper 中,所以我知道提交正在工作,只是我的响应处理程序不起作用。
这是我到目前为止的代码,我猜测 success 函数不是处理响应的正确方法。
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
success: function( data, status ) {
alert("yay");
},
error: function(status) {
alert("oh noes");
}
});
return false;
添加文章后,Instapaper 返回 201。我可以在 Google Chrome 网络工具中看到 GET 返回了 201 状态。只是想知道我如何在上面的代码中处理该状态。
谢谢。
编辑 当我单击链接激活下面的代码时,它会立即在错误函数下弹出更改,即使它已经起作用了。
最佳答案
jQuery.ajax() 为此目的提供了 statusCode 映射:
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
statusCode: {
200: function( data ) {
alert("yay");
},
201: function( data ) {
}
},
error: function(status) {
alert("oh noes");
}
});
关于javascript - 处理 HTTP 状态 Ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590871/