2

I know this question is asked more often here on Stack, but I don't seem to get a working answer out of all the questions asked.

The question "How to abort an AJAX call?" is mostly answered with the following solution (I use jQuery):

var call = $.ajax({
    url: 'path/to/destination',
    type: 'GET',
    dataType: 'json'
})
.done(function() {
    console.log("Done!");
})
.fail(function() {
    console.log("Error!");
})

To abort the call just do: call.abort(); But that doesn't seem to work. (While it should, right?!).

When I trigger the abort() function, it gives back an object, but my request is still pending in the network tab of dev tools. Once the server is done processing my request it gives it back regardless of the abort() function.

According to my research, it can't be stopped that way, because the request is already sent and received by the server and therefore can't be aborted. But shouldn't there be a way to stop waiting on the server and disregard the info it would give back or just cut the connection of the AJAX call in question?

Guido Visser
  • 2,209
  • 5
  • 28
  • 41
  • This will help you http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery – itzMEonTV Mar 16 '15 at 08:48
  • @latheefitzmeontv The OP already mentions the `call.abort()` so I think the problem is somewhere else. – t.niese Mar 16 '15 at 08:50
  • What does this [fiddle](http://jsfiddle.net/dgnLazzm/1/) show to you in the console and the network tab? – t.niese Mar 16 '15 at 08:52
  • @t.niese it says canceled – Guido Visser Mar 16 '15 at 08:54
  • 1
    And the console `Error` or `Done` ? – t.niese Mar 16 '15 at 08:55
  • 3
    *If nothing else works*, you can perhaps set a custom property in the xhr (eg: `cancelled`) while aborting and check it inside `done()` to ensure you aren't acknowledging the response of an aborted request. – techfoobar Mar 16 '15 at 08:55
  • @t.niese 2 times Error! once by `fiddle.jshell.net/:33` and once by `(index):33` – Guido Visser Mar 16 '15 at 08:56
  • 1
    As you can see `call.abort` works, but I guess in your case there is already some data send by the server at the time you call `call.abort()`, e.g. some headers, or already some part of the body, and you can't abort the request as of that. – t.niese Mar 16 '15 at 09:02
  • That's right! Thanks for saying that, but the problem remains; how to stop/cancel any information retrieved by the server? It's fine that some headers where sent back, but I just don't want to execute any code in the `done` or `error` functions. – Guido Visser Mar 16 '15 at 09:05
  • 1
    You can e.g. add a `context` to your ajax request, where you store the information if you abort it and check this information in the `done` or `error` callback. While jQuery adds an abstraction layer so that you don't need to care about different browser behaviors it still tries to be low level, as of that everything related to managing requests or other higher level logic, needs to be solved by your code. But just ignoring a successful response is a bad idea. – t.niese Mar 16 '15 at 09:13

1 Answers1

2

What .abort() actually does is it just ignores the response:

http://www.w3.org/TR/XMLHttpRequest/#the-abort()-method

You are right that you cannot abort waiting for the response. This is against HTTP spec and it could potentially lead to weird issues (when cancelling parallel requests). All you can do is ignore it.

But that's not a problem since waiting for the response is very light-weight operation. No need to be worried.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • I tried it out, but it still triggers the `done` function when it's done. So this doesn't work – Guido Visser Mar 16 '15 at 09:07
  • 1
    @GuidoVisser As already mentioned in other comments: that is not related to `.abort()`. You either do the abort too late (the response manages to come back) or you have some serious mess in the code and actually you abort different request. When you abort a request jQuery will fire `error` with `status` equal to `'abort'`. You can handle that case. – freakish Mar 16 '15 at 10:01
  • Thanks for your response, but see the last paragraph of my question. I'll see a way around it and will probably use variables to check if it should execute the done/error function. Something like Techfoobar has mentioned in the comments – Guido Visser Mar 16 '15 at 11:12
  • 1
    @GuidoVisser Wait, you are trying to solve an issue that **doesn't** exist or at least shouldn't exist. The code you've posted **does not** does what you say it does. It's like your saying "my roof is leaking, please help me with removing all of that water", no, you need to fix the roof. It looks you are trying to abort a request **after** you get response. That's conceptually wrong. – freakish Mar 16 '15 at 14:16