【问题标题】:How to get response data from the server in angularjs如何在angularjs中从服务器获取响应数据
【发布时间】:2015-04-19 19:59:11
【问题描述】:

这是我的场景。 nodejs 中的服务器处理身份验证过程,而前端我们有 angularjs。当用户单击按钮时,他使用 Facebook 登录,然后服务器处理身份验证的所有方面,最后重定向到 angularjs 应用程序的 uri。我们在服务器中有类似的东西

module.exports = function(request, reply) {

  if (request.auth.isAuthenticated) {

    var profile = request.auth.credentials.profile.raw;

    // set to cookie
    request.auth.session.set(profile);

    // Perform any account lookup or registration, setup local session,
    // and redirect to the application. The third-party credentials are
    // stored in request.auth.credentials. Any query parameters from
    // the initial request are passed back via request.auth.credentials.query

    // here we should redirect the app flow somewhere
    return reply({ profile: profile.id }).redirect('http://localhost:8080/app');
  }

  return reply('Unauthorized').code(401);
};

我的问题是我不知道如何在 angularjs 中检索配置文件对象。我的意思是我知道存在 $http 提供程序,但在以下情况下,请求不是从 angularjs 开始的。如果用户签名成功,则服务器会使用 SPA 回复流程


$http.get('/app')
  .success(function(data){
     console.log(data);
   });

【问题讨论】:

  • 加载 SPA 后,您仍必须使用 $http 获取配置文件数据。
  • 好的,但是应该通过哪个 uri?
  • 我的意思是我一直希望 $http 提供程序开始调用,但在这种情况下,流程是不同的。服务器为 SPA 服务
  • 我应该使用 /app 吗?
  • 在这两种情况下,SPA 都是从服务器提供的。所以流量不变。您必须重定向到应用程序 uri /app

标签: angularjs node.js hapijs


【解决方案1】:

您可以使用 $routeProvider 将其作为 URL 参数发送

您的配置应如下所示:

app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/login/:profileId', {
        templateUrl: 'template.html',
        controller: 'loginCtrl'
      }).
      otherwise({
        redirectTo: '/home'
      });
  }]);

你的控制器:

app.controller("loginCtrl",function($routeParams,$scope){
   $scope.profileId = $routeParams.profileId;
   //YOU CAN REDIRECT HERE TO ANOTHER VIEW AFTER
})

后端

module.exports = function(request, reply) {

  if (request.auth.isAuthenticated) {

    var profile = request.auth.credentials.profile.raw;

    // set to cookie
    request.auth.session.set(profile);

    // Perform any account lookup or registration, setup local session,
    // and redirect to the application. The third-party credentials are
    // stored in request.auth.credentials. Any query parameters from
    // the initial request are passed back via request.auth.credentials.query

    // here we should redirect the app flow somewhere
    return reply({ profile: profile.id
    // use # for html5 mode 
  }).redirect('http://localhost:8080/app/#/login/'+profile.id);
  }

  return reply('Unauthorized').code(401);
};

【讨论】:

  • 谢谢。这比我想象的要容易
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 2015-10-09
相关资源
最近更新 更多