2010-09-01 56 views
9

你好,我注意到,这个简单的代码不工作,它应该工作的方式......

function test() { 
    $.ajax({ 
     'url' : 'test/GameConfiguration.json', 
     'dataType' : 'json', 
     data : { 
      a : 'aaa' 
     }, 
     cache : false, 
     method : 'get', 
     timeout : 10000, //10 secs of timeout 
     success : function(data, textStatus, XMLHttpRequest) { 
      console.log("success"); 
      if (data == null) 
       console.log("it's not a real success"); 
     }, 
     error : function(XMLHttpRequest, textStatus, errorThrown) { 
      console.log("error: " + textStatus); 
     } 
    }); 
} 

测试已在本地主机上运行,我的意思是:我加载页面,关闭本地web服务器,然后我发起请求(通过一个简单的按钮,onclick指向这个函数)。错误不会被调用,我得到的是成功处理程序调用,它有textStatus =“success”和data = null。我甚至注意到请求在10秒之前超时。 这发生在Firefox(最新版本),Chrome(最新版本)和Safari 5.这是为什么?是否因为我在本地主机上工作?


我忘了告诉:请求没有被缓存。事实上,萤火虫和Chrome开发工具都会显示失败的要求。


大更新

此行为与使用本地主机。事实上,如果我从另一个同事PC加载此页面,并在触发请求之前断开我的PC与网络的连接,我将错误处理程序正确地启用,并将超时设置为状态。我认为这是jQuery的一个bug。这会让我很难测试超时错误:(


家伙从jQuery的论坛上说,这是由于网络堆栈中止连接的方式,给定主机为localhost,我测试了在Windows 7只是如果你觉得测试这个在其他系统上,你可以在jQuery的论坛,制定出一些jQuery的内部,报告这个帖子:

http://forum.jquery.com/topic/strange-and-unexpected-behaviour-of-ajax-error-and-localhost#14737000001331961

+2

做为什么你'“url''和'”引述字符串dataType''? – Codesleuth 2010-09-01 10:00:39

+0

我试图删除引号:没有区别 – gotch4 2010-09-01 10:02:06

+0

你可以为此显示一个演示页面吗? – Nalum 2010-09-01 10:14:35

回答

7

UPDATED:尝试将(data == null)替换为(XMLHttpRequest.readyState === 4 && XMLHttpRequest.status === 0)

在W3C Candidate关于XMLHttpRequest的推荐标准中描述了它必须存在如此命名的“错误标志”,它应该用来指示某种类型的网络错误或堕胎。如果发生此类错误,XMLHttpRequest中的状态将为0而不是200(“OK”),201(“创建”),304(“未修改”)等。与http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute完全相符,在XMLHttpRequest.readyState等于0或1(“UNSENT”或“OPENED”)的情况下,XMLHttpRequest.status可以为0。另一种情况是XMLHttpRequest.readyState等于4(“完成”)并且“错误标志”为真。如果我们没有这两种情况,则XMLHttpRequest.status必须是HTTP状态码。在http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.htmlhttp://www.w3.org/Protocols/HTTP/1.0/spec.html#Status-Codes没有HTTP状态码等于0,所以它似乎我的,你可以按照

jQuery(document).ready(function() { 
    $.ajax({ 
     type: 'GET', 
     url: 'test/GameConfiguration.json', 
     dataType: 'json', 
     cache : false, 
     success: function(data, textStatus, xhr){ 
      if (xhr.readyState === 4 && xhr.status === 0) { 
       // if readyState is DONE (numeric value 4) then corresponds to 
       // http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-done 
       // "The DONE state has an associated error flag that indicates 
       // some type of network error or abortion." 
       // Corresponds to http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute 
       // If error flag it true, xhr.status is 0. 
       alert('"error flag\" is true. It means that we have a network error"+ 
         " or abortion (for example because of Same Origin Policy restrictions)'); 
      } 
      else { 
       alert(textStatus); 
       alert(data); 
      } 
     }, 
     error: function(xhr, textStatus, errorThrown) { 
      if (textStatus !== null) { 
       alert("error: " + textStatus); 
      } else if (errorThrown !== null) { 
       alert("exception: " + errorThrown.message); 
      } 
      else { 
       alert ("error"); 
      } 
     } 
    }); 
}); 
+0

谢谢,这听起来更健壮。无论如何,我把所有这些发布在jQuery论坛上,一些开发者开始关注这个。我甚至发送了一些调试日志,可能这个东西会被修复。 – gotch4 2010-09-02 09:02:48

+0

@ gotch4:我在jQuery bug报告中找不到你的文章。你能发表参考吗?我发现了一些其他类似的报道,如http://dev.jquery.com/ticket/6060。看起来,尝试为Opera提供解决方法的更改http://dev.jquery.com/changeset/6432错误地实现了,并且现在在其他浏览器中XMLHttpRequest.status = 0的情况下工作错误。如果你不这样做。我会重申你在bug报告中插入参考文献http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute。 jQuery的代码至少应遵循标准,然后尝试实施任何解决方法。 – Oleg 2010-09-02 14:38:01

-2

即使你关闭本地服务器,它应该还是能够看到json文件..尝试暂时删除文件,看看是否有效。

+0

如果我删除了文件并且服务器处于开启状态,当然我会得到一个404错误处理程序并触发。 如果我删除文件并且服务器关闭,结果相同。 – gotch4 2010-09-01 10:14:47

1

如果发生网络错误,将调用success回调,而不是error,即使它没有多大意义。详情请参阅this blog post。所以基本上你通过检查data == null是正确的。

+0

请看我的答案和代码示例。你如何解读结果?你认为我们有jQuery中的错误吗? – Oleg 2010-09-01 13:29:01

+0

不,您的测试是错误的:您正在更改端口号。由于同源策略限制,这是不允许的:http://en.wikipedia.org/wiki/Same_origin_policy。如果更改端口号,则无法执行AJAX查询。 – 2010-09-01 14:19:06

+0

你是对的,我的第一个例子应该被解释为相同的“原产地策略限制”错误。我根据http://www.w3.org/TR/XMLHttpRequest编写新版本的代码,在我看来,这两种情况都可以检测到错误:“原始策略限制”或任何类型的网络错误。在所有这些情况下,“成功”处理程序的调用应该被解释为错误。你对这个建议有什么看法? – Oleg 2010-09-01 18:13:49