How to handle session timeout error on ajax call in asp.net mvc3
Possible Duplicate:
Handling session timeout in ajax calls
I want to handle timeout error on ajax call where i have set session timeout to 1 minute like..
<authentication mode="Forms">
forms loginUrl="~/User/Login" timeout="1" />
/authentication>
and my call for every ajax request is like.. $.ajax({
url: '../Document/getData?did=' + docid2,
beforeSend: function () {
$("#DialogData").empty().html('<p class="Loadimg"></p>');
},
success: function (result) {
$("#DialogData").load('../Document/getData?did=' + docid2);
},
error: function (req, status, xhr) {
alert(xhr);
//for Timeout Error
if (status == "timeout") {
$("#DialogData").empty().html('<p>Plese Try Again</p><');
}
if (status == "error") {
//for Page Not Found Invalid URL
if (xhr == "Not Found") {
}
//for server error which is from controller
if (xhr == "Internal Server Error") {
}
} //if error
} //error
}); //Ajax
but on timeout i get internal server error...i want to handle timeout error differently than other error so what should i do..where i can differntiate this error.because now if session timeout it gives internal server error or if any other server error occure it gives same internal server error as xhr value..when session timeout it display that login page which is redirect from controller is in div where i am calling $("#DIVID").load(..)..
..i want to redirect as soon as session timout on load to entire login page... how can i do...thanks in advanced..
my code from controller side for redirection is..
/// <summary> /// The Logout method Removes the Authentication Ticket from the browser /// </summary> /// <returns>Redirection to the Login Page of Site</returns> [Authorize] public ActionResult Logout() { FormsAuthentication.SignOut(); return RedirectToAction("Login", "User"); } public bool IsUserLoggedIn() { if (!String.IsNullOrEmpty(System.Web.HttpContext.Current.User.Identity.Name)) { return true; } return false; } public int GetLoggedInUserId() { if (!IsUserLoggedIn()) return -1; return Convert.ToInt32(System.Web.HttpContext.Current.User.Identity.Name); }
I would recommend you the following article which illustrates how you could configure ASP.NET to send a 401 status code if the forms authentication ticket expires. This allows you to intercept this status code in your AJAX request and take respective actions.
But if you are getting a 500 error code that means that there is something wrong with your server side code that you will have to fix first.
链接地址: http://www.djcxy.com/p/71794.html上一篇: 在ajax调用中处理会话超时