我试图给Petfinder.com打电话以获取我们的宠物列表。网址是http://api.petfinder.com/shelter.getPets?key=xxxxx&id=CA1469&format=jsonJQuery ajax调用获取状态代码0“错误”
该网址似乎返回的JSON罚款。但是当我尝试拨打电话时,我收到了“错误”和状态码0.我尝试使用jsonp,导致状态为200,但发生分析错误。如果我更改为XML,我有状态0和“错误”的结果。
$.ajax({
url: "http://api.petfinder.com/shelter.getPets?key=xxxx&id=CA1469&format=json",
dataType: "json",
type: "GET",
success: function (data) {
alert("hi");
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
您的控制台中是否出现“相同原点”错误?你使用IE吗?跨域请求需要发送无论是作为JSONP(需要适当的JSONP响应)或CORS启用这就需要服务器支持CORS并支持CORS的浏览器。 jQuery的不支持CORS在IE <10 – 2013-02-13 18:56:24
Trello的API客户端库提供的,他们如何处理CORS支持一个很好的例子:[阿贾克斯jqXHR.status的https://trello.com/1/client.coffee – forivall 2013-02-13 19:07:05
可能重复== 0修复错误](http://stackoverflow.com/questions/23802474/ajax-jqxhr-status-0-fix-error) – 2015-07-09 11:31:42