Is there a way to handle the error from an ajax request within JQuery such that when the user navigates away from the page, the resulting error is ignored?
$(document).ready(function() {
$.ajax( {
url:'/server/url/',
type: 'POST',
data: {some..data},
success:function(response) { do stuff with response },
error: function(xhr, textStatus, errorThrown) {
// Don't raise this alert if user has navigated away from the page
alert('error');
}
});
I’m curious if anybody knows of a clean way to handle this? I know you could create some logic in a $(window).bind("beforeunload", function(){}) method but I’d prefer if there was a way to handle the situation without doing this since Chrome appears to no longer support it. Is there for example a specific error that is returned in this case?
Thanks.
It looks like the answer to this is to examine the jqXHR.status. The XMLHttpRequest spec outlines these steps to set the status:
NOTE also:
The error flag indicates some type of network error or request abortion. It is initially unset and is used during the DONE state.
From what I understand therefore, this code check should fix the issue:
1https://web.archive.org/web/20120204040047/http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute