如何按照AngularJS在http.get重定向? [英] How to follow a redirect in http.get in AngularJS?

查看:159
本文介绍了如何按照AngularJS在http.get重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的教练在网站上,以帮助人们拥有更健康的生活,通过使用社交媒体。在那一刻我的工作在防爆pressJS服务器访问到Twitter通过OAuth的在前端AngularJS。

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的网站

From the website of AngularJS:

200和299之间的响应状态code被认为是成功的状态,并会导致成功回调被调用。请注意,如果响应是一个重定向,XMLHtt prequest会透明地遵循它,这意味着错误回调将不会被要求对这种反应。

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.

我的服务器的完整code可以在 web.js 并 twitter.js 在github上,但这是来电:

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

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

这是web.js被调用:

And this is the callee in 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);      
    }
  });
});

如果我请与的tcpdump (对不起我老土)我看到:

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

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

这是非常好的,当然,事实上当我去到该服务器 /会话/连接在浏览器中手动发生。然而,随着AngularJS我的浏览器的屏幕实际上不是重定向。为什么不呢?

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?

推荐答案

$ HTTP 进入并获取页面的内容为您服务。但是,它给你的结果早在承诺。它不会在所有的,除非你要求它改变你的页面。在你的然后处理,你可以做一些事情来重定向页面。还有的你如何能做到的是,这里的例子:<一href=\"http://stackoverflow.com/questions/16629057/handle-an-ex$p$pss-redirect-from-angular-post\">Handle一个前preSS从角POST重定向

$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

这篇关于如何按照AngularJS在http.get重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆