title: Handle redirects in angularjs $http
body: <p>I'm using angularjs http service in my app, and I noticed this in the <a href="http://tutorials.jenkov.com/angularjs/ajax.html" rel="nofollow noreferrer">website</a>:</p>
  
  <blockquote>
    <p>If the AJAX call succeeds (the server sends back an HTTP code between
    200 and 209), the function passed to the success() function is
    executed. If the AJAX call fails (all other codes except for
    redirects), the function passed to the error() method is executed.</p>
  </blockquote>
  
  <p>Obviously, the redirects status code isn't part of success neither error callbacks. So how are we going to handle redirects? </p>
  
  <p>This is what my script does, get data from server using http get. If session expires, server returns <em>302 status code</em>. I should catch that status code then redirect the page to login.</p>
  
  <pre><code>app.controller('GraphController', function($scope, $http, localStorageService, $interval) {
          $scope.GraphData = [{"key":"in","values":[]},{"key":"out","values":[]}]; 
  
          $scope.pollStats = function() {
              $http.get('/statistics/getStat').success(function(lastData, status, headers) {              
                  if (status==302) {
                      //this doesn't work
                      //window.location=headers('Location');
                      //this doesn't work either
                      window.location.replace(headers('Location'));
                  }else if (status==200) {
                      ...processData
                  }
              });
          };
  });
  </code></pre>
  
  <p>Obviously my script won't work because success callback can't handle redirects. So how are we supposed to handle that?</p>