【发布时间】:2011-01-31 05:31:58
【问题描述】:
我收到了一份无法复制的错误报告,但 ajax 调用超时是目前最好的猜测。
所以我试图找出 jQuery $.ajax() 调用的超时默认值。有人有想法吗?在 jQuery 文档中找不到它。
提前致谢, 马库斯
【问题讨论】:
-
嗯,我认为这是特定于浏览器的。
标签: jquery ajax timeout default-value
我收到了一份无法复制的错误报告,但 ajax 调用超时是目前最好的猜测。
所以我试图找出 jQuery $.ajax() 调用的超时默认值。有人有想法吗?在 jQuery 文档中找不到它。
提前致谢, 马库斯
【问题讨论】:
标签: jquery ajax timeout default-value
请参考此 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.
【讨论】:
XMLHttpRequest.timeout 属性表示请求在自动终止之前可能需要的毫秒数。默认值为0,表示没有超时。一个重要的注意事项超时不应用于同步XMLHttpRequests 请求,在文档环境中使用,否则它将引发InvalidAccessError 异常。对于具有拥有窗口的同步请求,您不能使用超时。
IE10 和 11 不支持同步请求,其他浏览器也将逐步取消支持。这是由于 detrimental effects 造成的。
更多信息可以在here找到。
【讨论】:
默认情况下没有超时。
【讨论】:
顺便说一句,当我尝试诊断一个类似的错误时,我意识到如果 jquery 的 ajax 错误回调由于超时而失败,它会返回“超时”状态。
这是一个例子:
$.ajax({
url: "/ajax_json_echo/",
timeout: 500,
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus); // this will be "timeout"
}
});
【讨论】:
似乎没有标准化的默认值。我感觉默认是0,而超时事件完全取决于浏览器和网络设置。
对于 IE,XMLHTTPRequests here 有一个超时属性。它默认为 null,它表示网络堆栈可能是第一个超时的(顺便说一下,这不会生成一个 ontimeout 事件)。
【讨论】: