-2

I have many called ajax request from many js file. I want to detect few ajax request to interrupt the execution and execute a newer one. can I detect and stop the ajax with their url?

I would be greatly appreciative of any help I could gain.

mister
  • 95
  • 6

1 Answers1

1

Here is code adapted from this post. It's untested, but should give the idea. It stuffs the xhr call into an array, along with URL, and then calls them back out to kill them based on URL.

var url = "some.php",
   xhr_global = [];

var jqxhr = $.ajax({
    type: "POST",
    url: url,
    data: "name=John&location=Boston",
    success: function(msg){
       alert( "Data Saved: " + msg );
    }
});

xhr_global.push({xhr: jqxhr, url: url});

function kill_url(url_pick){

    for(var i=0; i<xhr_global.length; i++){
        if (xhr_global[i].url == url_pick)     // If url matches, kill xhr
            xhr_global[i].xhr.abort();
    }

}

kill_url("some.php");     // Call function to kill the xhrs

Note that, as the post says, this kills the process on client-side, but not server-side.

Community
  • 1
  • 1
PeterM
  • 439
  • 2
  • 9
  • Thank you for your answer. This is if I have an access to the declaration of the ajax. But if I have not access to his declaration and have just the url, how can I do it – mister Apr 25 '16 at 15:02
  • If you don't have access to the declaration of the ajax (eg., you can't save the jqxhr 'promise'), then I don't believe you can stop the ajax call. – PeterM Apr 25 '16 at 15:57
  • thank you for your answer. it is very helpful but I have to abort an ajax called by my pc and I can't access to the code. – mister Apr 26 '16 at 07:12