2010-06-08 23 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