简体   繁体   中英

How to check if ajax request was aborted by browser

I'm making long polling ajax requests to my server

$.ajax({
    url: "someurl.com", 
    success: function(resp) { ... },
    error: function() { ... }
});

but if there is some inte.net connection problems for a short period of time Firefox 10 aborts the request and hence my script doesn't receive response from the server (in older versions of FF there were no such request aborting).

How can I found out that my ajax request was aborted by browser? Maybe there are some event for it? Or can I force browser not to abort my ajax requests if the.network connection is down?

For XMLHTTPRequest, it gives status "0" saying that request not initialized . You can catch that status and know that req has been aborted.

Perhaps you could add a timeout to the call?

  $.ajax({
        url: "test.html",
        error: function(){
            // will fire when timeout is reached
        },
        success: function(){
            //do something
        },
        timeout: 3000 // sets timeout to 3 seconds
    });

Check the abort status of the jqXHR in the.fail (previously.error) function.

let request = $.ajax({
    // your options
})

request.fail(jqXHR,textStatus){
    if(jqXHR.abort){
        alert("Aborted")
    }else{
        alert("Other error")
    }
})

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