How do I stop jQuery POST?

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?

+4
source share
3 answers
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.

+2
source

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

0
source

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;}); 
0
source

All Articles