簡體   English   中英

是否可以中止數據類型為json的jquery $ .ajax請求?

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

我想在窗口unload事件中取消數據類型為json的jquery $.ajax呼叫請求。 我嘗試這樣做,它在xhr.abort();xhr.abort();錯誤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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM