6

I'm using jquery for making ajax requests

$.ajax(
    url: "http://someurl.org",
    success: function() { ... }
);

How can I manually stop my particular ajax request?

tsds
  • 8,700
  • 12
  • 62
  • 83

2 Answers2

22

The $.ajax() method returns an object that could be used to do that:

var xhr = $.ajax(
    url: "http://someurl.org",
    success: function() { ... }
);

and then later when you want to stop the request:

xhr.abort();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    ajax can be stopped from being initiated, but once its made its not possible to stop it... – JIA Feb 21 '12 at 08:31
  • 4
    @JIA, you can abort the AJAX request in the browser but of course if it has already hit the web server, that's a whole different story. The web server will continue to execute the request. It's just that since the browser aborted the request the web server will send the response to the void. – Darin Dimitrov Feb 21 '12 at 08:34
  • @DarinDimitrov so it means that if an already initiated request is aborted using the `.abort` method it will not be catered for at the client side ... – JIA Feb 21 '12 at 08:46
  • @JIA, that will depend when you call the abort method. – Darin Dimitrov Feb 21 '12 at 08:51
3

ajax requests are HTTP transactions once made cannot be stopped. You can stop ajax request from being initiated but not kill the already made request.

JIA
  • 1,485
  • 13
  • 11
  • 2
    XHR requests, like all HTTP requests, can be aborted while underway. Whether a certain Ajax library exposes this functionality is a different issue. – GregT May 02 '14 at 18:55