Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a request was aborted?

I am making a request and then right after it I abort.

var x = $.get(url, function (d, e, xhr) { alert(d); });
x.abort();

The problem is that it executes the success function and returns empty data... (example here)

Is there a jQuery method to abort? or Is there a way to check if the xhr was aborted?

like image 893
BrunoLM Avatar asked Sep 05 '10 23:09

BrunoLM


2 Answers

The best way to detect request abortion and avoiding false positive from offline mode :

$("#loading").ajaxError(function(event, xhr) {
  if (xhr.status === 0) {
    if (xhr.statusText === 'abort') {
      // Has been aborted
    } else {
      // Offline mode
    }
  }
});
like image 70
fdaugan Avatar answered Oct 03 '22 10:10

fdaugan


I found here that the xhr will return with status 0. Seems to be a jQuery 1.4+ bug. On 1.3 it called the error handler.

like image 44
BrunoLM Avatar answered Oct 03 '22 08:10

BrunoLM