Reputation: 75073
Nowadays we tend to split the amount of work made in just one page before this one renders, to speed things up, so, we normally load the basic and then upon DOM is ready, we call all parts to bring more data showing the normal animated loading gif ...
What is the normal procedure to, when requesting for such method, the user session already expired and he/she needs to login again?
Currently I have no such thing and the browser only shows endless the loading gif... I was wonder what would be the best approach to such trivial problem.
I thought about sending an error flag that, from javascript I could read and redirect to the login page
$.get( url, data, function(data) {
if(data.logout) document.location.href = '/LogOut';
...
});
but I get nothing from my controller soon the session expired...
P.S. My web application is made using ASP.NET MVC 3.
Upvotes: 2
Views: 2159
Reputation: 1038710
I would recommend you start with the following article in which Phil Haack explains how to properly handle 401 errors in ASP.NET MVC without automatically redirecting you to the LogOn
page.
Then simply do your AJAX requests as normal:
$.get(url, data, function(data) {
// if we got here it means that the request succeeded
// => do whatever you have to do for this case
});
and then you could register a global AJAX event to handle the 401 case for all AJAX requests:
$(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
if (jqXHR.status == 401) {
// the server returned 401 (Unauthorized) =>
// redirect to the LogOn page
window.location.href = 'put your logon url here';
}
});
Upvotes: 4