Created
November 16, 2012 06:48
-
-
Save rainyjune/4084886 to your computer and use it in GitHub Desktop.
The best way to retry an AJAX request on failure using jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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
good Job