为什么在asp.net mvc会话超时后jquery ajax调用失败?
当我的会话变量中有一个值时,我的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(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 = "" } ); }
尝试重定向任何响应:
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
返回的内容
之后再回到我身边……
如果Session已超时,则Session对象将为null。 您正在尝试访问此对象,而不检查它是否存在将抛出exception。
你有一个“onError”回调函数设置吗?