2012-01-19 35 views
0

可能重複:
Handling session timeout in ajax calls如何在AJAX調用處理會話超時錯誤在asp.net MVC3

我想處理在哪裏我已設置會話超時AJAX調用超時錯誤到1分鐘就像..

<authentication mode="Forms"> 
    forms loginUrl="~/User/Login" timeout="1" /> 

/authentication>

我呼籲每一個Ajax請求就像.. $.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(..).. ..我想盡快會議timout負載得益於先進重定向到整個登錄頁面... 我該怎麼辦......

我從控制器端重定向代碼..

/// <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); 
    } 
+0

有沒有這樣的事,作爲一個會話超時錯誤。您的服務器端代碼根本就沒有重新創建會話變量像它應該。 –

回答

0

我會建議你following article它說明如何能配置ASP.NET如果窗體身份驗證票證過期發送401個狀態碼。這使您可以在您的AJAX請求攔截此狀態碼,並採取相應的行動。

但是,如果你得到一個500錯誤代碼,這意味着有什麼不對您的服務器端代碼,你將不得不首先解決。

+0

我更新了我的question..can存在的任何解決方案... –