Reputation: 61
I have an application, which retrieves some page data via $.ajax call on page load (no other bindings, like 'click' etc.). In a scenario, when user reloads page (or navigates to another page) before the response arrives, the application returns an error (error event ajaxError() is triggered) with status code 0.
When I was doing my homework and read around SO (and lots of other places) I came across the posts where people simply ignored errors of this kind by doing the following:
$(document).ajaxError(function(e, jqxhr, settings, exception) {
if (jqxhr.readyState == 0 || jqxhr.status == 0) {
return; //Skip this error
}
});
Is it safe to presume that only this kind of errors will be ignored? Can any negative scenarios appear that would need 'special attention'?
ALSO:
I've found some replies to similar issues stating that putting 'return false;' or 'preventDefault' solved their issue. But in my case everything is happening on page load, so I can't 'hook' on any of mentioned events... Or can I?
EDIT: The additional question here is: is there any other way of handling these situations (ajax request being interrupted by page reload)? The thing is that I have error reporting bound on the general ajaxError event and I'd like to filter out messages like this (or maybe even prevent them from happening in the first place)?
Upvotes: 3
Views: 1590
Reputation: 1681
In response to your question "is it safe", my answer is, yes it is safe. I have been using it since past couple of months and it works great for me.
However, below api documentation shades more information on error object. I haven't moved to jQuery 1.9* yet, but it's still good.
For more information: http://api.jquery.com/jQuery.ajax/#jqXHR
Upvotes: 1
Reputation: 9924
This will ignore all errores returned from ajax calls. It's sort-of-safe: users will never notice that the error ocurred. Hard to say outside any concrete example, but your application will probably break when it does an AJAX call that fails, unless each handler handles the error accordinly.
Upvotes: 1