【问题标题】:JQuery ajax call default timeout valuejQuery ajax 调用默认超时值
【发布时间】:2011-01-31 05:31:58
【问题描述】:

我收到了一份无法复制的错误报告,但 ajax 调用超时是目前最好的猜测。

所以我试图找出 jQuery $.ajax() 调用的超时默认值。有人有想法吗?在 jQuery 文档中找不到它。

提前致谢, 马库斯

【问题讨论】:

标签: jquery ajax timeout default-value


【解决方案1】:

请参考此 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.

【讨论】:

    【解决方案2】:

    XMLHttpRequest.timeout 属性表示请求在自动终止之前可能需要的毫秒数。默认值为0,表示没有超时。一个重要的注意事项超时不应用于同步XMLHttpRequests 请求,在文档环境中使用,否则它将引发InvalidAccessError 异常。对于具有拥有窗口的同步请求,您不能使用超时。

    IE10 和 11 不支持同步请求,其他浏览器也将逐步取消支持。这是由于 detrimental effects 造成的。

    更多信息可以在here找到。

    【讨论】:

      【解决方案3】:

      默认情况下没有超时。

      【讨论】:

      • 您能否分享一些支持您的主张的来源。
      • 我刚刚测试过,可以确认。
      • 如何测试没有超时?默认情况下可能很长​​span>
      • 有些人还在测试:/
      【解决方案4】:

      顺便说一句,当我尝试诊断一个类似的错误时,我意识到如果 jquery 的 ajax 错误回调由于超时而失败,它会返回“超时”状态。

      这是一个例子:

      $.ajax({
          url: "/ajax_json_echo/",
          timeout: 500,
          error: function(jqXHR, textStatus, errorThrown) {
              alert(textStatus); // this will be "timeout"
          }
      });
      

      Here it is on jsfiddle.

      【讨论】:

      【解决方案5】:

      似乎没有标准化的默认值。我感觉默认是0,而超时事件完全取决于浏览器和网络设置。

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

      【讨论】:

      • 所以基本上 jQuery 不使用任何默认值。该问题已在 Safari 上报告。我想我会尝试强制一些较长的超时值并希望最好......谢谢!
      • @Marcus :它真的有效吗?我找不到超时是否总是覆盖浏览器的超时值(即使您的 ajax 超时值大于浏览器的)
      • @AdrienBe 当时 IIRC 我无法从用户的错误报告中复制问题,但超时问题是目前最好的猜测。所以我真的没有答案。
      猜你喜欢
      • 2011-05-08
      • 1970-01-01
      • 2012-07-17
      • 2017-12-25
      • 2016-09-24
      • 2012-05-05
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多