title: How to follow a redirect in http.get in AngularJS?
body: <p>I am working on a website for coaches to help people have healthier lives by using social media. On the moment I am working on access to Twitter through OAuth in a ExpressJS server with AngularJS in the frontend.</p>
  
  <p>From the <a href="http://docs.angularjs.org/api/ng.$http" rel="noreferrer">website of AngularJS</a>:</p>
  
  <blockquote>
    <p>A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.</p>
  </blockquote>
  
  <p>The full code of my server can be found in <a href="https://github.com/mrquincle/sensorcoach/blob/master/web.js" rel="noreferrer">web.js</a> and <a href="https://github.com/mrquincle/sensorcoach/blob/master/assets/js/twitter.js" rel="noreferrer">twitter.js</a> on github, but this is the caller:</p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>function LoginCtrl($scope, $http, $location) {
    $scope.welcome = 'Sign in with Twitter';
    $http.defaults.useXDomain = true;    
    $scope.submit = function() {    
      $http({method: 'GET', url: '/sessions/connect'}).
      success(function(data, status) {
          $scope.welcome = data;
      });
    };
  }
  </code></pre>
  
  <p>And this is the callee in web.js:</p>
  
  <pre class="lang-js prettyprint-override"><code>app.get('/sessions/connect', function(req, res){
    consumer().getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
      if (error) {
        res.send("Error getting OAuth request token : " + sys.inspect(error), 500);
      } else {  
        req.session.oauthRequestToken = oauthToken;
        req.session.oauthRequestTokenSecret = oauthTokenSecret;
        res.redirect("https://api.twitter.com/oauth/authorize?oauth_token="+req.session.oauthRequestToken);      
      }
    });
  });
  </code></pre>
  
  <p>If I check with <code>tcpdump</code> (sorry I'm old-fashioned) I see:</p>
  
  <pre class="lang-js prettyprint-override"><code>Connection:.keep-alive....Moved.Temporarily..Redirecting.to.https://api.twitter.com/oauth/authorize?oauth_token=***
  </code></pre>
  
  <p>This is really nice of course, and indeed happens when I go to this server <code>/sessions/connect</code> manually in the browser. However, with AngularJS my browser screen is not actually redirected. Why not?</p>