简体   繁体   中英

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. I tried doing this and it is throwing error on 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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