1

I have the following problem in Chrome: if I click anchor at page during ajax request (which redirects me to other page), error handler is invoked. I'd like to determine whether it is real error or not.

  • abort handler is not invoked
  • error is empty
  • xhr.status is 0
  • xhr.statusText is "error"

Code:

$.ajaxSetup({
    error: function (xhr, status, error) {
        ...
    },
    abort: function () {
        ...
    }
});
Marat Faskhiev
  • 1,282
  • 6
  • 17
  • 37

1 Answers1

3

Use onbeforeunload event to abort all request before page is unloaded. This way you will get status = 'abort' instead of status = 'error' in error: function(xhr, status, error){}.

var request;

window.onbeforeunload = function() {
    if(request !== undefined) {
        request.abort();
    }
}

request = $.ajax({ ... });

Example I've tested this solution on:

http://jsfiddle.net/vLrYm/7/

Konrad Dzwinel
  • 36,825
  • 12
  • 98
  • 105