在asp.net mvc中,为什么jquery ajax调用在会话超时后失败?
当我的会话变量中有一个值时,我的ajax调用会正常工作。。。但是当会话是timedout时,它似乎无法返回空的json结果在asp.net mvc中,为什么jquery ajax调用在会话超时后失败?,jquery,asp.net-mvc,ajax,redirect,session-timeout,Jquery,Asp.net Mvc,Ajax,Redirect,Session Timeout,当我的会话变量中有一个值时,我的ajax调用会正常工作。。。但是当会话是timedout时,它似乎无法返回空的json结果 public JsonResult GetClients(int currentPage, int pageSize) { if (Session["userId"]!=null) { var clients = clirep.FindAllClients(Convert.ToI
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);
}
}
编辑:
我的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 = "" }
);
}
如果会话超时,则会话对象将为null。您试图访问此对象而不检查它是否存在,这将引发异常
是否设置了“onError”回调函数?尝试重定向响应:
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 ' + );
}
}
您还可以查看数据。重定向URL
返回的内容
请稍后再联系我…当会话超时时,它实际上在做什么?它是否返回了一个JSON对象?@matthew ya它返回了一个JSON对象,但没有重定向到我的登录视图,而是停留在同一个视图中…使用Firebug在Firefox中打开页面,并在警报(data.Results)行上放置一个断点,然后您应该能够检查该对象。通常对于javascript,如果发生错误,当前脚本块的执行将终止。您需要确定您试图访问的属性是否在对象中实际可用。另外,尝试“location.href”看看这是否有区别。@Matthew我在我的回复中找到了这个
{“redirectUrl”:“/”,“isRedirect”:true}
。。在我看来,它就像你的Url。操作(“创建”,“注册”)并没有返回你期望的结果。还有,你有任何脚本错误吗?是的,我没有“onError”回调函数设置。。。我应该在“onError”回调函数中做什么?我是否应该将我的用户重定向到登录页面。当出现错误时,请执行您需要执行的任何操作,但对于开发,我建议您可能需要以某种方式向屏幕发出警报。
success: function(data) {
if (data.isRedirect && data.isRedirect === true) {
alert('must redirect to ' + data.redirectUrl);
location = 'http://www.google.ca';
}
else
{
alert('not redirecting ' + );
}
}