Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Created November 16, 2012 06:48
Show Gist options
  • Save rainyjune/4084886 to your computer and use it in GitHub Desktop.
Save rainyjune/4084886 to your computer and use it in GitHub Desktop.
The best way to retry an AJAX request on failure using jQuery
$.ajax({
url : 'someurl',
type : 'POST',
data : ....,
tryCount : 0,
retryLimit : 3,
success : function(json) {
//do something
},
error : function(xhr, textStatus, errorThrown ) {
if (textStatus == 'timeout') {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
$.ajax(this);
return;
}
return;
}
if (xhr.status == 500) {
//handle error
} else {
//handle error
}
}
});
@ahmed-bhs
Copy link

good Job

@OtavioBraga
Copy link

Thx xD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment