5

I would like to stop an ajax call in jquery, which is not working

var xhr = null;

xhr = $.ajax({
    url : 'www.example.com?some-large-call',
    success : function(responseText) {
        // some DOM manipulation
    }
});

$("#button").click(function() { xhr.abort() });

I referred the below link

http://www.stoimen.com/blog/2009/09/29/jquery-stop-an-ajax-call/

2
  • Have you checked the console during this process? If so what does it say?
    – BenM
    Commented Mar 3, 2014 at 13:39
  • Always define 'not working'!!! Because i guess it works but not as you expect it. So what do you expect?
    – A. Wolff
    Commented Mar 3, 2014 at 13:44

3 Answers 3

3

this probally has more XHR..

Please see this answer:

jquery abort() ajax request before sending another

From the link:

every time you do ajax request, add to array:

requests.push(
$.ajax({
    type: 'post',
    url: '/test.php',
    data: search_data,
    success: function(data) {
        $('#catalog').html(data);
    }
}));

Then, when you want to stop calls.. loop through array and abort all calls..

 for(var i = 0; i < requests.length; i++)
requests[i].abort();

Thanks

5
  • 1
    Link answers are discouraged. Commented Mar 3, 2014 at 13:42
  • i dont have enuf rep to add comment to anything else :( - i can delete if ok?
    – JF it
    Commented Mar 3, 2014 at 13:43
  • 1
    No need to delete, just add a summary to this answer. Commented Mar 3, 2014 at 13:43
  • 1
    @JFit you should have voted to close question because of duplicate
    – A. Wolff
    Commented Mar 3, 2014 at 13:44
  • Sorry wolff.. not sure how to do this.. I update my comment..
    – JF it
    Commented Mar 3, 2014 at 13:46
3

xhr.abort() it will cause the client to stop listening for the event, but probably it may not stop the server from processing it.

0

Provided the code you've pasted is the code you're actually using it's no wonder it doesn't work.

As it states on the site it is just pseudo code to illustrate an example. There is no AJAX request there and nothing to stop (at least nothing large enough to be able to stop)

Not the answer you're looking for? Browse other questions tagged or ask your own question.