I have a jQuery ajax post where I upload large files using POST. I want to give the user the ability to cancel sending by clicking the button while sending is in progress. How can i do this?
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.
Take a look at this blog post explaining the XMLHTTPRequest.Abort () method. I believe that this is what you are looking for.
In the 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;});