1

我在 Chrome 中有以下问题:如果我在 ajax 请求期间单击页面上的锚点(将我重定向到其他页面),则会调用错误处理程序。我想确定它是否是真正的错误。

  • 未调用中止处理程序
  • 错误为空
  • xhr.status 为 0
  • xhr.statusText 是“错误”

代码:

$.ajaxSetup({
    error: function (xhr, status, error) {
        ...
    },
    abort: function () {
        ...
    }
});
4

1 回答 1

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/

于 2012-07-26T10:01:17.550 回答