1

我想在窗口事件上取消数据类型为 json 的 jquery$.ajax调用请求。unload我试过这样做,它xhr.abort();在线抛出错误。

var xhr = $.ajax({         
    type: "POST",
    url: serviceUrl,
    dataType: "text json",
    data: ajaxParameters,
    async: true,
    contentType: "application/json; charset=utf-8",
    error: function(request, status, error) {
    },
    complete: function(e, xhr, settings) {                      
    }
});

$(window).onunload = function(){
    xhr.abort();
}
4

1 回答 1

0

试试这个...

// Global variable
var xhr;

xhr = $.ajax({         
    type: "POST",
    url: serviceUrl,
    dataType: "text json",
    data: ajaxParameters,
    async: true,
    contentType: "application/json; charset=utf-8",
    error: function(request, status, error) {
    },
    complete: function(e, xhr, settings) {                      
    }
});

$('body').on('beforeunload',function(){
    xhr.abort();
}
于 2013-01-29T06:27:21.093 回答