开发者

XMLHttpRequest error 0 proper management (retry the request)

开发者 https://www.devze.com 2022-12-18 19:37 出处:网络
When XMLHttpRequest gets error 0 it means there is no connection and most of the times you just want to retry in some time.

When XMLHttpRequest gets error 0 it means there is no connection and most of the times you just want to retry in some time.

Is there a simple way to transparent开发者_JS百科ly retry the XMLHttpRequest notifying the user the connection is lost (just like gmail does)?

(I am using jQuery)


Look here for what you're after: Defensive AJAX and AJAX retries in jQuery.

Basically in the error handler, you can do $.ajax(this);

$.ajax({
  url : 'timeOutPage.html',
  type : 'get',
  timeout : 20000,
  error : function(xhr, textStatus, errorThrown ) {
     if (textStatus == 'timeout') {
       $.ajax(this);
       return;
     }
  }
 });

The linked article has a more in-depth example with a retry limit, etc...as with most jQuery, you can almost copy/paste their example to get going.

0

精彩评论

暂无评论...
验证码 换一张
取 消