jQuery Ajax调用默认超时时间值

78

我收到了一个无法重现的错误报告,但 ajax 调用超时是目前的最佳猜测。

因此,我正在尝试查找 jQuery $.ajax() 调用的超时默认值。有人有想法吗?我在 jQuery 文档中找不到它。


1
嗯,我认为这是特定于浏览器的。 - Pekka
1
https://dev59.com/enM_5IYBdhLWcg3whznx - Adriano
https://dev59.com/o2025IYBdhLWcg3wyZEn - Adriano
1
可能是重复的问题:jQuery 的 ajax 默认超时时间是多少? - Pang
5个回答

52

似乎没有标准的默认值。我感觉默认值为0,超时事件完全依赖于浏览器和网络设置。

对于IE,XMLHTTPRequests有一个超时属性,在这里。它默认为null,并且说网络栈很可能是第一个超时的(顺便说一下,这不会生成ontimeout事件)。


所以基本上jQuery不使用任何默认值。这个问题在Safari上报告了。我想我只需要尝试强制设置一些较长的超时值,然后希望一切顺利...谢谢! - Marcus
1
@Marcus:它真的起作用了吗?我找不到超时是否总是覆盖浏览器的超时值(即使您的ajax超时值大于浏览器的)。 - Adriano
@AdrienBe 当时我记得我无法从用户的错误报告中复制问题,但超时问题是当时最好的猜测。所以我真的没有答案。 - Marcus

23
作为旁注,当尝试诊断类似的错误时,我意识到如果由于超时而失败,jQuery的ajax错误回调将返回一个“timeout”的状态。
这是一个例子:
$.ajax({
    url: "/ajax_json_echo/",
    timeout: 500,
    error: function(jqXHR, textStatus, errorThrown) {
        alert(textStatus); // this will be "timeout"
    }
});

在 jsfiddle 上的演示


是的,请查看所有错误类型 https://dev59.com/n3A65IYBdhLWcg3w9DmF - Adriano

7

默认情况下没有超时。


2
你能分享一些支持你说法的来源吗? - Panther
我刚刚测试过了,可以确认这一点。 - Diego Jancic
9
如何测试超时时间是否不存在?默认情况下它可能会非常长。 - Kaktus
5
一些人仍在测试 :/ - metamagikum
1
@metamagikum 我还没有放弃!虽然已经过去了将近8年,但我相信我随时都会收到那个超时错误! - Ted

2
XMLHttpRequest.timeout属性表示请求在自动终止之前可以花费的毫秒数。默认值为0,这意味着没有超时。重要提示:超时不应用于同步的XMLHttpRequests请求,在文档环境中使用会抛出InvalidAccessError异常。您不能在具有拥有窗口的同步请求中使用超时。
IE10和11不支持同步请求,在其他浏览器中也逐渐停止支持。这是由于制作它们会产生有害影响
更多信息可在此处找到。

0
请参考此API信息。
timeout
Type: Number
Set a timeout (in milliseconds) for the request. A value of 0 means there 
will be no timeout. This will override any global timeout set with 
$.ajaxSetup(). The  timeout period starts at the point the $.ajax call is made; 
if several other  requests are in progress and the browser has no connections 
available, it is  possible for a request to time out before it can be sent. In 
jQuery 1.4.x and  below, the XMLHttpRequest object will be in an invalid state if 
the request  times out; accessing any object members may throw an exception. In 
Firefox 3.0+  only, script and JSONP requests cannot be cancelled by a timeout; 
the script  will  run even if it arrives after the timeout period.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接