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 ?
Asked
Active
Viewed 1,580 times
4
-
1You could use this: http://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery – Frias Jul 15 '11 at 17:43
3 Answers
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
-
I use the .ajaxForm jquery plugin. I don't know how to access the xhr object. – Teodor Pripoae Jul 15 '11 at 18:26
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
-
-
Yes it will. As I said it is to handle the worst case even after the post successfully happened but user cancelled it. – ShankarSangoli Jul 15 '11 at 17:55
-
that is indeed worst case, because, in some cases you would need to send a "undo" to the server – beardhatcode Jul 15 '11 at 17:56
-