title: Ajax session timeout
body: <p><strong>Situation :</strong></p>
  
  <p>My web application is password protected.For each <strong>http</strong> request we make to server, it is being checked against session existence. If session has been expired then the user is forwarded to <strong>login</strong> page.
  This goes fine for http requests. But if it is an AJAX request, then just like http request, if session has been expired, it is also forwarded to login page.</p>
  
  <p><strong>Problem :</strong></p>
  
  <p>if we are directly showing AJAX response in browser, then in place of our expected response will show the login page content in your browser.And if you would be fetching data of any expected format, then it would throw JavaScript error.</p>
  
  <p><strong>My Code :</strong></p>
  
  <pre><code>&lt;script&gt;
  function details() {
  var xyz = document.getElementById("name").value;
  // Returns successful data submission message when the entered information is stored in database.
  var dataString = 'name=' + xyz;
  
  // AJAX code to submit form.
  $.ajax({
  type: "POST",
  url: "user.php",
  data: dataString,
  cache: false,
  success: function(html) {
  document.getElementById("content").innerHTML=html;
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
      if (XMLHttpRequest.status === 401) {
            location.href = 'index.php';
          }
  }
  });
  return false;
  }  
  &lt;/script&gt;
  </code></pre>
  
  <p>on Session time out when i call Ajax request through my code. It loads the <strong>login.php</strong> content into the current page instead of forwarded the user to <strong>login Page</strong>.</p>
  
  <p>Guide me where i am doing something wrong.<br>
  Thanks.</p>