2

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 = "" } 
           );
        }
7
  • What's it actually doing when the Session has timed out? Is it returning a JSON object at all? Commented Jun 8, 2010 at 7:20
  • @matthew ya it returns a json object but it doesn't get redirected to my Login view instead it stays in the same view... Commented Jun 8, 2010 at 7:45
  • Open your page in Firefox with Firebug and put a breakpoint on the on the alert(data.Results) line, you should then be able to inspect the object. Typically with javascript, if an error occurs, executing of the current script block will terminate. You need to find out if the properties your are trying to access are actually available in the object. Also, try just "location.href" to see if that makes a difference. Commented Jun 8, 2010 at 7:48
  • @Matthew i found this {"redirectUrl":"/","isRedirect":true} in my response.... Commented Jun 8, 2010 at 9:16
  • Looks to me like your Url.Action("Create", "Registration") isn't returning what you are expecting. Also, are you having any script errors? Commented Jun 8, 2010 at 9:30

2 Answers 2

2
+100

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

Sign up to request clarification or add additional context in comments.

Comments

0

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?

2 Comments

ya i dont have an "onError" callback function setup... What should i do in that "onError" callback function? Should i redirect my user to login page..
Do whatever you need to do when an error occurs, but for development I would suggest that you probably need to alert it to the screen somehow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.