是否可以使用数据类型 json 中止 jquery $.ajax 请求

Is it possible to abort jquery $.ajax request with data type json?

本文关键字:jquery ajax 请求 中止 json 可以使 数据类型 是否      更新时间:2023-09-26

我想取消 jquery $.ajax 在窗口unload事件上具有数据类型 json 的调用请求。我尝试这样做,但它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();
}

试试这个...

// 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();
}