Skip to content

Instantly share code, notes, and snippets.

@balaramm
Forked from rainyjune/gist:4084886
Created October 12, 2020 12:32
Show Gist options
  • Select an option

  • Save balaramm/663f528948bd21a5ed6d89420edec221 to your computer and use it in GitHub Desktop.

Select an option

Save balaramm/663f528948bd21a5ed6d89420edec221 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
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment