简体   繁体   English

是否可以中止数据类型为json的jquery $ .ajax请求?

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

I want cancel jquery $.ajax call request with datatype json on window unload event. 我想在窗口unload事件中取消数据类型为json的jquery $.ajax呼叫请求。 I tried doing this and it is throwing error on xhr.abort(); 我尝试这样做,它在xhr.abort();xhr.abort();错误xhr.abort(); line. 线。

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

Try this out... 试试看...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM