I have an AJAX action that can take a couple of minutes to complete depending upon the amount of data involved. If a user gets frustrated and navigates away while this action is still running, what happens to the controller? Does it complete anyway? Does it know the request should be abandoned and dispose of the controller object?
-
3I think the most accurate answer is "maybe", and the approach is to assume "yes". The server-side code was invoked and is processing. Canceling the request (*if* the browser notifies the server of anything) may notify the web server at the HTTP level that it can be canceled, but that doesn't necessarily mean your application code is going to stop executing. The application code could easily complete as usual and the web server will just ignore the response. Or the server can respond to the client and the client will just ignore the response. Either way, the controller is disposed normally. – David Sep 19 '14 at 13:50
-
Dispose is called after the view is rendered in _any_ case - in your scenario the controller will still completely render the view, but the rendered view will just not get sent back to the client. – D Stanley Sep 19 '14 at 13:50
-
Definitely the controller would have no way to know that the user left the website and will continue the execution. The response will probably be ignored by the browser – Catalin Sep 19 '14 at 13:51
-
Even if the server knew that the client browsed away, what should it do? Thread.Abort is evil and can't be used. Execution continues and the response is discarded. – usr Sep 19 '14 at 14:02
1 Answers
It will not cancel the request to the server as the act of navigating away does not send any information back to the server regarding that request. The client (browser), however, will stop listening for it. After the request finishes, regardless if the client was listening for it or not, the controller will dispose as it typically would.
With that said, you could get fancy and use a combination of listening for a page change on the client side and calling abort
on the AJAX request to the server.
This SO question discusses how to abort a request. I could imagine setting a variable when you first start the AJAX request and then unsetting it when it finished.
Warning - Pseudo code below
var isProcessing = false;
var xhr = $.ajax({
type: "POST",
url: "myUrl",
beforeSend: function(){
isProcessing = true;
}
complete: function(){
isProcessing = false;
}
});
window.onbeforeunload = function(){
if(isProcessing){
xhr.abort();
}
}
The above is very basic idea of the concept, but there should probably be some checks around if the xhr object exists, perhaps also bind/unbind the window.onbeforeunload
in the beforeSend and complete object handlers for the .ajax()
item.
-
Thanks for your response. The answer to this SO question seems to imply that abort() won't terminate the server-side action. Is that true? http://stackoverflow.com/questions/4551175/how-to-cancel-abort-jquery-ajax-request – Raymond Saltrelli Sep 19 '14 at 14:10
-
Compliments to Erv Walter of the Question that Tommy posted in his Answer "Of note, if the server has already received the request, it may continue processing the request (depending on the platform of the server) even though the browser is no longer listening for a response. There is no reliable way to make the web server stop in its tracks with processing a request that is in progress" – CSharper Sep 19 '14 at 14:25
-
1@RaySaltrelli - I have to agree with C Sharper that there just isn't a definitive answer. I would say, its safe to assume that once the server receives the request, it will most likely continue on doing what it was told to do. I think the difference in all of the information out there is browser, server, and code related. With that said, there is some sparse information on how you could cancel the request using Tasks and Cancellation tokens, however, I have relatively little experience with this. https://github.com/ermagana/AsyncCancelExample – Tommy Sep 19 '14 at 14:31