为什么在asp.net mvc会话超时后jquery ajax调用失败?

时间:2010-06-08 06:59:49

标签: jquery asp.net-mvc ajax redirect session-timeout

当我的会话变量中有一个值时,我的ajax调用正常工作......但是当一个会话超时时,它似乎无法返回空的json结果....

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 = "" } 
           );
        }

2 个答案:

答案 0 :(得分:2)

尝试重定向响应:

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

如果重定向,则可以消除这种可能性

然后尝试明确地比较变量并暂时保持URL的硬编码:

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返回的内容

之后再回到我身边......

答案 1 :(得分:0)

如果Session超时,则Session对象将为null。您正在尝试访问此对象,而不检查它是否存在将抛出异常。

您是否有“onError”回调函数设置?