简体   繁体   English

即使HTTP 200状态,jQuery Ajax也会失败

[英]jQuery Ajax fails even with the HTTP 200 status

As the title sais, I have this Javascript code: 正如标题所说,我有以下Javascript代码:

$.ajax({
      type: "GET",
      url: "http://192.168.20.249/test.brick",
      dataType: "json"
      }).done(function() { alert("success"); }).fail(function() { alert("error"); }).always(function() { alert("complete"); });

My webserver sends these data: 我的网络服务器发送这些数据:

200 OK
Date:  Tue, 12 Mar 2013 07:49:51 GMT

Server:  Hiawatha v8.8

Connection:  keep-alive

Transfer-Encoding:  chunked

Content-Type:  application/json

{"Test": "Hello"}

Any idea why jQuery thinks the request failed? 知道为什么jQuery认为请求失败了吗?

as your jquery script server and web server are different, it should be a cross-domain issue. 由于您的jquery脚本服务器和Web服务器不同,因此应该是跨域问题。

You should use jsonp for this communication. 您应该使用jsonp进行此通信。

Below is an example which can help you 以下是可以帮助您的示例

http://www.jquery4u.com/json/jsonp-examples/ http://www.jquery4u.com/json/jsonp-examples/

Maybe you can use the error callback to get som more info of what error jQuery sees? 也许您可以使用error回调获取有关jQuery看到的错误的更多信息?

$.ajax({
    type: "GET",
    url: "http://192.168.20.249/test.brick",
    dataType: "json",
    error: function(jqXHR, textStatus, errorThrown){
        ("error: " + textStatus); //Check all parameters in this callback
    },
    success: function(){
        alert("success"); 
    }
});

Edit: Cross domain ajax is not really as convenient as one would wish. 编辑:跨域ajax并不像人们希望的那样方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM