如何在AngularJS中的http.get中进行重定向?

时间:2022-08-22 17:46:33

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.

我正在建立一个教练网站,通过使用社交媒体帮助人们过上更健康的生活。目前我正在使用前端AngularJS的ExpressJS服务器中的OAuth访问Twitter。

From the website of AngularJS:

来自AngularJS的网站:

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.

200到299之间的响应状态代码被视为成功状态,并将导致调用成功回调。请注意,如果响应是重定向,则XMLHttpRequest将透明地跟随它,这意味着不会为此类响应调用错误回调。

The full code of my server can be found in web.js and twitter.js on github, but this is the caller:

我的服务器的完整代码可以在github上的web.js和twitter.js中找到,但这是调用者:

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;
    });
  };
}

And this is the callee in web.js:

这是web.js中的被调用者:

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);      
    }
  });
});

If I check with tcpdump (sorry I'm old-fashioned) I see:

如果我查看tcpdump(对不起,我是老式的),我看到:

Connection:.keep-alive....Moved.Temporarily..Redirecting.to.https://api.twitter.com/oauth/authorize?oauth_token=***

This is really nice of course, and indeed happens when I go to this server /sessions/connect manually in the browser. However, with AngularJS my browser screen is not actually redirected. Why not?

这当然非常好,当我在浏览器中手动访问此服务器/ sessions / connect时确实会发生这种情况。但是,使用AngularJS,我的浏览器屏幕实际上并未重定向。为什么不?

1 个解决方案

#1


6  

$http goes and gets the contents of the page for you. However, it gives you the results back in the promise. It doesn't change your page at all unless you ask it to. In your then handler, you can do something to redirect your page. There's an example of how you can do that here: Handle an express redirect from Angular POST

$ http会为您获取并获取页面内容。但是,它会在承诺中为您提供结果。除非您要求,否则它根本不会更改您的页面。在当时的处理程序中,您可以执行一些操作来重定向页面。这里有一个如何做到这一点的例子:处理Angular POST的快速重定向

#1


6  

$http goes and gets the contents of the page for you. However, it gives you the results back in the promise. It doesn't change your page at all unless you ask it to. In your then handler, you can do something to redirect your page. There's an example of how you can do that here: Handle an express redirect from Angular POST

$ http会为您获取并获取页面内容。但是,它会在承诺中为您提供结果。除非您要求,否则它根本不会更改您的页面。在当时的处理程序中,您可以执行一些操作来重定向页面。这里有一个如何做到这一点的例子:处理Angular POST的快速重定向