views:

88

answers:

3

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?

+1  A: 

EDIT: Try this:

x.onreadystatechange = null;
x.abort();

Seems to work. Not sure what side effects, if any.


Original answer:

Would it be sufficient to just test the response received?

var x = $.get("./", function (d, e, xhr) {
    if(d) {
        // run your code with response
        alert(d);
    }
    // otherwise, nothing will happen
});
patrick dw
+1  A: 

This is by design. Test if data is null to determine if the request responded correctly.

If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method.

It may be useful to handle this (from here).

Codesleuth
This `.ajaxError` doesn't seems to work. Can you provide an example?
BrunoLM
I don't think `ajaxError` will help. After `abort()` you get a successful empty response.
patrick dw
+2  A: 

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.

BrunoLM
Good find... +1
patrick dw