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

发布于 2024-09-04 21:04:37 字数 1790 浏览 6 评论 0原文

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

when there is a value in my session variable my ajax calls work properly... But when a session is timedout it doesn't seem to work returning empty json result....

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

        }

However else part does'nt seem to work....

   success: function(data) {
       alert(data.Results);
        if (data.isRedirect) {
            window.location.href = data.redirectUrl;
        }
   }

EDIT:

My global.asax has this,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏林苍穹下 2024-09-11 21:04:37

尝试重定向无论响应是什么:

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 返回

Comke之后回到我身边...

Try to redirect whatever the response is:

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

if that redirects, you can eliminate that possibility

Then try to explicitely compare the variable and keep the URL hardcoded for the moment:

success: function(data) {
    if (data.isRedirect && data.isRedirect === true) {
        alert('must redirect to ' + data.redirectUrl);
        location = 'http://www.google.ca';
    }
    else
    {
        alert('not redirecting ' + );
    }
}

You can also see what data.redirectUrl returns

Comke back to me after that...

未蓝澄海的烟 2024-09-11 21:04:37

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

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

If the Session has timed out out then the Session object will be null. You are attempting to access this object without checking if it exists which will be throwing an exception.

Do you have an "onError" callback function setup?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文