Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest error 0 proper management (retry the request)

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 transparently retry the XMLHttpRequest notifying the user the connection is lost (just like gmail does)?

(I am using jQuery)

like image 438
Eduardo Avatar asked Jan 30 '10 22:01

Eduardo


1 Answers

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.

like image 193
Nick Craver Avatar answered Sep 22 '22 21:09

Nick Craver