jquery ajax中,断网和超时

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

阅读 8.9k
2 个回答

来自 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.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进