【问题标题】:Why jquery ajax calls fails after session timeout in asp.net mvc?为什么在asp.net mvc中的会话超时后jquery ajax调用失败?
【发布时间】:2010-06-08 06:59:49
【问题描述】:

当我的会话变量中有一个值时,我的 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 = "" } 
           );
        }

【问题讨论】:

  • 当 Session 超时时它实际上在做什么?它是否返回一个 JSON 对象?
  • @matthew ya 它返回一个 json 对象,但它不会重定向到我的登录视图,而是保持在同一个视图中......
  • 使用 Firebug 在 Firefox 中打开您的页面并在 alert(data.Results) 行上放置一个断点,然后您应该能够检查该对象。通常使用 javascript,如果发生错误,当前脚本块的执行将终止。您需要确定您尝试访问的属性是否在对象中实际可用。另外,试试“location.href”看看是否有区别。
  • @Matthew 我在回复中发现了这个{"redirectUrl":"/","isRedirect":true}....
  • 在我看来,您的 Url.Action("Create", "Registration") 没有返回您所期望的。另外,您是否有任何脚本错误?

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


【解决方案1】:

尝试重定向任何响应:

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

在那之后回到我身边......

【讨论】:

    【解决方案2】:

    如果 Session 已超时,则 Session 对象将为空。您正在尝试访问此对象而不检查它是否存在,这将引发异常。

    你有“onError”回调函数设置吗?

    【讨论】:

    • 是的,我没有“onError”回调函数设置......我应该在那个“onError”回调函数中做什么?我应该将我的用户重定向到登录页面吗..
    • 发生错误时做任何你需要做的事情,但对于开发我建议你可能需要以某种方式提醒它到屏幕上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2012-05-22
    • 2015-12-19
    • 2011-08-12
    相关资源
    最近更新 更多