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 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.
精彩评论