title: Handling session timeouts in ajax calls
body: <p>ASP.Net MVC 5  .Net Framework 4.6.1</p>
  
  <p>I just added code to detect session timeout which works fine:</p>
  
  <pre><code>     public class CheckSessionTimeOutAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
      {
          var context = filterContext.HttpContext;
          if (context.Session != null)
          {
              if (context.Session.IsNewSession)
              {
                  string sessionCookie = context.Request.Headers["Cookie"];
                  if ((sessionCookie != null) &amp;&amp; (sessionCookie.IndexOf("ASP.NET_SessionId", StringComparison.Ordinal) &gt;= 0))
                  {
                      string redirectTo = "~/Home/Index";
                      filterContext.HttpContext.Response.Redirect(redirectTo, true);
                  }
              }
          }
          base.OnActionExecuting(filterContext);
      }
  }
  </code></pre>
  
  <p>As you can see, I redirect them to the home screen. I have my [CheckSessionTimeOut] as an attribute on all pertinent controllers. So, I run the app, go to a page other than the home screen, wait 1 minute for session timeout, the code runs as expected in certain situations. Case and point, I have a dropdown and when a selection is made, a redirect is taking place. Heres the method:</p>
  
  <pre><code>   $('#selusers').change(function () {
      var rd = $(this).find("option:selected").attr('redirect');
      location.href = rd;
  });
  </code></pre>
  
  <p>What happens here is when a user is selected from the dropdown, the redirect attribute is read and redirection to that person takes place. If the session times out, redirection to the logged in user takes place and not the newly selected user. This is correct for my app.</p>
  
  <p>However, I make numerous ajax calls in my app. When the session times out and I click on an element that fires an ajax call, I get redirected to the home screen, but the error method gets called in the ajax request. I get a popup with the home screen html inside of it. Here's one example of an ajax call I'm making. I'm on a screen with a save button, the session times out and this code gets fired:</p>
  
  <pre><code>    SaveButtonClicked: function (somedata) {
              $.ajax({
                  url: url,
                  type: 'POST',
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  data: JSON.stringify(some data im sending),
                  success: function (dataResult) {
                          if (!dataResult.Ok) {
                              alert("Error occurred. Please try again");
                          }
                  },
                  error: function (err) {
                      alert(err.responseText);//this gets called
                  }
              });
          }
  </code></pre>
  
  <p>I have overridden the default alert so styled html will appear. The alert box has the home screen html in it. I do not want that. I want the app to be redirected to the home screen, no popup. So my first question, is there a way to do something at the server to stop the ajax call from running its complete methods(i.e I want to do something at the server so the ajax call's success, error, or complete methods will never be called)? Next question if the first one isn't an option, what would be a very good way to detect that the session has timed out in the complete methods of an ajax call so the user is gracefully redirected to the home screen? If there are any other ways of doing what i'm trying to achieve that I didn't not ask, please share them. Thank you for reading my question.</p>
  
  <p><b>UPDATE</b>
  I removed the error function from one of my ajax calls and added a global error handler instead:</p>
  
  <pre><code>   $(document).ajaxError(function (e, xhr, settings) {
          debugger;
       });
  </code></pre>
  
  <p>Now, when the session timeout, this error handler gets called, BUT, i look in the xhr variable and it contains the results of the ajax call and corresponding html. The status is a 200, the statusText says parsererror, the readystate is a 4. There is nothing here that tells me the session timed out. What can i do at the global ajaxError method to tell me session timed out?</p>