2013-02-13 95 views
5

我试图给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); 
      } 

     } 
    }); 
+1

您的控制台中是否出现“相同原点”错误?你使用IE吗?跨域请求需要发送无论是作为JSONP(需要适当的JSONP响应)或CORS启用这就需要服务器支持CORS并支持CORS的浏览器。 jQuery的不支持CORS在IE <10 – 2013-02-13 18:56:24

+0

Trello的API客户端库提供的,他们如何处理CORS支持一个很好的例子:[阿贾克斯jqXHR.status的https://trello.com/1/client.coffee – forivall 2013-02-13 19:07:05

+0

可能重复== 0修复错误](http://stackoverflow.com/questions/23802474/ajax-jqxhr-status-0-fix-error) – 2015-07-09 11:31:42

回答

1

您正在运行到 XSS CORS问题。最好的解决方案是在您的Web服务器上设置代理,并通过它来路由AJAX调用。

+1

正如KevinB所说;我认为你的意思是CORS,而不是XSS – Alexander 2013-02-13 19:01:55

+0

@Alexander同意。 – 2013-02-13 19:03:14

+0

我使用AWS API网关对我机应用...并获得这个同样的问题...你可以建议优选CORS政策,使得这个错误得到解决。我ryte测试它现在使用运行在body标签的“的onload”事件一个简单的Ajax调用 – 2016-08-16 07:39:15

2

您需要确保当服务器向您发送错误响应时,它还会发送适当的CORS头文件。

如果没有浏览器不会让你的JavaScript代码来看到任何信息来源来自该域,即使状态正在添加的代码。