开发者

How should I stop a "in progress" jquery POST?

开发者 https://www.devze.com 2023-03-20 10:26 出处:网络
开发者_C百科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 thi

开发者_C百科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 ?


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 that explains the XMLHTTPRequest.Abort() method. I believe that is what you are looking for.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号