title: Handle 302-redirect in Jquery
body: <p>I have backbone application in which I need to take care if the user lost a session. When the session is lost, server sends 302 (redirect to login page). That works in some cases, and in other it does not.</p>
  
  <p>I have made a ajax request that ping the server in intervals confirming the session. I have read the many topics on SO, such as this <a href="https://stackoverflow.com/questions/15996779/cannot-handle-302-redirect-in-ajax-and-why">one</a></p>
  
  <p>And there it says, that browser handles 302's.</p>
  
  <p>I am confused here.
  This is the simple Ajax request that I use to ping the server:</p>
  
  <pre><code>setInterval(function () {
          if(baseHref){
              $.ajax({
                  type: 'GET',
                  url: baseHref + "api/ping",
                  success: function (data) {                        
                      if(data !== true){
  
                          window.location.href = baseHref + "login";
                      }
                  },
                  error: function (data) {
  
                  }
              });
          }
      },5000);
  </code></pre>
  
  <p>In local environment, when the session is lost, server sends 302 and I end up in success every time and I can make decision based on it.
  On the development environment (<a href="https://xxxxxx.com" rel="nofollow noreferrer">https://xxxxxx.com</a>) I never end up in any of the promises, Browser overrides them.</p>
  
  <p>One solution is to talk to server people and have them send me object, and not 302. But before I make my demand I would like to know:</p>
  
  <p>Why does it work in localhost? By all means it should not.</p>