首页 > jquery ajax中,断网和超时

jquery ajax中,断网和超时

现在有个问题,当用户在请求AJAX的时候,如果断网或者超时,应该怎么来设置jquery的$.ajax配置项


来自 jQuery Ajax 的文档,在使用 Ajax 方法的时候加个 timeout 参数,或者在 ajaxSetup 中设置全局参数。

注意单位:毫秒

timeout
Type: Number
Set a timeout (in milliseconds) for the request. 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.


首先,可以对 jQuery.ajax 进行超时设置,也就是配置超时时间。

在网络故障引起超时时,将会调用 failure 的回调。这时 Status Code0.

相关链接 : https://github.com/jquery/jquery/blob/master/src/ajax.js#L644

【热门文章】
【热门文章】