Making ajax call live across the actions
Am working on an MVC 3 web application and am in a situation where I would need to notify the user of the action result in a popup.
Right now am making an ajax call to the action when the user hits a button, if the user continues to work on anything else in the page the ajax on success is never called and am not able to receive the result back to the page.
What would be the best way to overcome this, so that whatever the user does in the page will not hamper the ajax call and still be able to display the result to the user.
Edit
Thanks for the quick responses,
I actually want the users to continue doing their work once they hit the button, since it would be an async email 开发者_高级运维operation. Once the operation is complete, I want to display the result in a div or using jQuery growl no matter what the user is doing
If the user navigates away from the page while there is an ongoing AJAX call there is nothing you could do to prevent it from stopping it. The best you could do is to subscribe for the onbeforeunload event and tell the user that there is an ongoing AJAX call and that if he continues he might lose his data:
window.onbeforeunload = function() {
return 'You are navigating away from the page but there is an ongoing operation that will be interrupted. Are you sure you want to continue?';
};
Of course you will initialize this handler only before sending an AJAX request and remove it when this request succeeds.
You could try displaying a full-size transparent (or better, semi-transparent) div overlay before the Ajax call and hide it when it completes.
That should stop users doing anything until the call returns.
精彩评论