4

I have a jQuery ajax post, where I upload big files with POST. I want to give to user the ability to cancel the submit, clicking a button, while the submit is in progress. How can I do this ?

Teodor Pripoae
  • 1,388
  • 1
  • 11
  • 26
  • 1
    You could use this: http://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery – Frias Jul 15 '11 at 17:43

3 Answers3

2
var xhr = null;

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

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

This should work for you

beardhatcode
  • 4,533
  • 1
  • 16
  • 29
0

Take a look at this blog post that explains the XMLHTTPRequest.Abort() method. I believe that is what you are looking for.

Ryan Gross
  • 6,423
  • 2
  • 32
  • 44
0

Worst case you can try something like this

var isRequestCancelled = false;

isRequestCancelled = false;

$.post({ url:"", success:function{
   if(isRequestCancelled){
     //Alert the user with cancel message
   }
   else{
     //Confirm the user
   }
}
});

//Show a cancel buttons once you post to server and on cancel click set the global variable.

$(".cancel").click(function(){ isRequestCancelled = true;});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124