tsds
tsds

Reputation: 9050

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 internet 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?

Upvotes: 0

Views: 2454

Answers (3)

mmarlow
mmarlow

Reputation: 264

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")
    }
})

Upvotes: -1

JBeagle
JBeagle

Reputation: 2660

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

Upvotes: 0

Ved
Ved

Reputation: 8767

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

Upvotes: 3

Related Questions