Why jquery ajax calls fails after session timeout in asp.net mvc?

Deadly 提交于 2019-12-23 01:05:36

问题


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

回答1:


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




回答2:


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?



来源:https://stackoverflow.com/questions/2995315/why-jquery-ajax-calls-fails-after-session-timeout-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!