如何从angularjs中获取服务器的响应数据

时间:2015-04-19 10:38:19

标签: angularjs node.js hapijs

这是我的情景。 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);
   });

1 个答案:

答案 0 :(得分:2)

您可以使用$ 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);
};