Friday, October 30, 2009

[jQuery] Re: Resending an XMLHttpRequest

> I'm wondering if there is a good way to resend an ajax request
> if it has produced an error.

Not that I know of, but it's an interesting question. The standard
XMLHttpRequest object doesn't have a way to resend the request. The
original $.ajax options are still around but they're not passed to the
global ajax event or the error callback so you can't manually submit
another $.ajax based on something the event or callback tells you,
unless you can grab it from the xhr.

All I could think of was something like this:

var myAjaxOptions = { ... };
$.ajax($.extend({}, myAjaxOptions, {
...
error: function(){
if ( we want to retry ) {
$.ajax($.extend({}, myAjaxOptions, {
...
error: function(){
alert("retry failed, bailing");
}
});
}
}
});
});

No comments: