为什么jQuery的Ajax调用在asp.net mvc的会话超时后失败? [英] Why jquery ajax calls fails after session timeout in asp.net mvc?

查看:166
本文介绍了为什么jQuery的Ajax调用在asp.net mvc的会话超时后失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时,有我的会话变量我的Ajax调用正常价值......但是,当一个会话TIMEDOUT它似乎并没有工作,返回空的JSON结果......

 公共JsonResult GetClients(INT当前页,诠释的pageSize)
        {
            如果(会话[用户id]!= NULL)
            {
                VAR的客户= clirep.FindAllClients(Convert.ToInt32(会话[用户id]))AsQueryable已()。
                VAR数= clients.Count();
                VAR的结果=新PagedList< ClientBO>(客户,当前第 -  1,pageSize的);
                VAR genericResult =新{数=计,结果=结果,isRedirect = FALSE};
                返回JSON(genericResult);
            }
            其他
            {
                VAR genericResult =新{的redirectUrl = Url.Action(创建,登记),isRedirect = TRUE};
                返回JSON(genericResult);
            }

        }
 

不过其他部分一点儿也不似乎工作....

 成功:功能(数据){
       警报(data.Results);
        如果(data.isRedirect){
            window.location.href = data.redirectUrl;
        }
   }
 

编辑:

我的Global.asax有这个,

 公共静态无效的RegisterRoutes(RouteCollection路由)
        {
            routes.IgnoreRoute({}资源个.axd / {* PATHINFO});

            routes.MapRoute(
                客户,
                客户端/ {行动} / {ID},
                新{控制器=客户端,行动=索引,ID =}
            );

            routes.MapRoute(
               注册,
               {控制器} / {行动} / {ID},
               新{控制器=注册,行动=创建,ID =}
           );
        }
 

解决方案

尝试重定向任何反应是:

 成功:功能(数据){
  位置='http://www.google.ca';
}
 

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

然后尝试比较明确地定义变量,并保持URL硬codeD的时刻:

 成功:功能(数据){
    如果(data.isRedirect&安培;&安培; data.isRedirect ===真){
        警报(必须重定向到'+ data.redirectUrl);
        位置='http://www.google.ca';
    }
    其他
    {
        警报('没有重定向'+);
    }
}
 

您还可以看到 data.redirectUrl 返回

Comke还给我之后...

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

解决方案

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...

这篇关于为什么jQuery的Ajax调用在asp.net mvc的会话超时后失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆