13

Is it possible to abort a previously-running Ajax request?

var xhr = $.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function(msg){
       alert( "Data Saved: " + msg );
    }
});
3
  • how about if(xhr){xhr.abort();}? Commented Nov 23, 2013 at 5:43
  • I think it is possible , it was available in ASP.NET AJAX toolkit
    – Devesh
    Commented Nov 23, 2013 at 5:43
  • thanks of all answers. now it solved Commented Nov 23, 2013 at 5:45

1 Answer 1

7

try something like this

        $(document).ready(function(){
            var xhr; // global object

            function your_function() {
                if(xhr && xhr.readystate != 4){
                    xhr.abort();
                }

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

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