2010-06-08 53 views
2

當在我的會話變量我的ajax調用正常工作的值...但是,當一個會話已逾時它似乎不工作返回空的JSON結果....爲什麼在asp.net mvc中會話超時後,jquery ajax調用失敗?

public JsonResult GetClients(int currentPage, int pageSize) 
     { 
      if (Session["userId"]!=null) 
      { 
       var clients = clirep.FindAllClients(Convert.ToInt32(Session["userId"])).AsQueryable(); 
       var count = clients.Count(); 
       var results = new PagedList<ClientBO>(clients, currentPage - 1, pageSize); 
       var genericResult = new { Count = count, Results = results ,isRedirect=false}; 
       return Json(genericResult); 
      } 
      else 
      { 
       var genericResult = new {redirectUrl = Url.Action("Create", "Registration"), isRedirect = true }; 
       return Json(genericResult); 
      } 

     } 

但是其他部分一點兒也不似乎工作....

success: function(data) { 
     alert(data.Results); 
     if (data.isRedirect) { 
      window.location.href = data.redirectUrl; 
     } 
    } 

編輯:

我的Global.asax有這個,

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       "Clients",            
       "Clients/{action}/{id}",       
       new { controller = "Clients", action = "Index", id = "" } 
      ); 

      routes.MapRoute(
       "Registrations",            
       "{controller}/{action}/{id}",       
       new { controller = "Registration", action = "Create", id = "" } 
      ); 
     } 
+0

當會話超時時它實際上在做什麼?它是否返回一個JSON對象? – 2010-06-08 07:20:06

+0

@matthew雅它返回一個JSON對象,但它不會被重定向到我的登錄視圖,而是它保持在同一視圖中... – 2010-06-08 07:45:52

+0

使用Firebug在Firefox中打開您的頁面,並在警報上放置一個斷點(數據。結果)行,您應該能夠檢查該對象。通常使用javascript,如果發生錯誤,當前腳本塊的執行將終止。您需要知道您嘗試訪問的屬性是否在對象中可用。 此外,請嘗試只是「location.href」,看看是否有所作爲。 – 2010-06-08 07:48:28

回答

2

嘗試重定向無論反應是:

success: function(data) { 
    location = 'http://www.google.ca'; 
} 

如果重定向,可以消除這種可能性

然後嘗試明確地比較變量,並保持硬編碼的時刻的網址:

success: function(data) { 
    if (data.isRedirect && data.isRedirect === true) { 
     alert('must redirect to ' + data.redirectUrl); 
     location = 'http://www.google.ca'; 
    } 
    else 
    { 
     alert('not redirecting ' +); 
    } 
} 

您還可以看到什麼data.redirectUrl回報

之後回到我身邊......

+0

你好,一旦我完成它,我會回到你的身邊... – 2010-06-16 04:16:24

+0

感謝它的工作... – 2010-06-16 09:01:15

+0

酷!現在很高興工作。 – 2010-06-16 11:51:57

0

如果會話已超時,則會話對象將爲空。你正試圖訪問這個對象而不檢查它是否存在,這將拋出一個異常。

你有一個「onError」回調函數設置?

+0

雅我沒有「onError」回調函數設置...我應該怎麼做,在「onError」回調函數?我應該將用戶重定向到登錄頁面嗎? – 2010-06-15 09:17:05

+0

在發生錯誤時做任何你需要做的事情,但對於開發,我建議你可能需要以某種方式警告它到屏幕上。 – starskythehutch 2010-06-15 11:49:43