简体   繁体   English

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

[英]How to get response data from the server in angularjs

This is my scenario. 这是我的情况。 A server in nodejs handles the authentication procedure while in the frontend we have angularjs. nodejs中的服务器处理身份验证过程,而前端则使用angularjs。 when the user clicks on the button, he signs in with Facebook then the server handles all the aspects of the authentication at the end redirect to the uri of the angularjs application. 当用户单击按钮时,他使用Facebook登录,然后服务器将处理身份验证的所有方面,最后重定向到angularjs应用程序的uri。 We have in the server something like that 我们服务器中有类似的东西

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

My issue is that I don't know how to retrieve profile object in angularjs. 我的问题是我不知道如何在angularjs中检索轮廓对象。 I mean I know that exist $http provider but in the following case the request doesn't start from angularjs. 我的意思是我知道存在$http提供程序,但在以下情况下,请求不是从angularjs开始的。 summering the flow the server reply with SPA if the user sign is successfully 如果用户签名成功,服务器将对SPA进行回复


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

you can send it as an URL Parameter using the $routeProvider 您可以使用$ routeProvider将其作为URL参数发送

your config should look like this: 您的配置应如下所示:

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

your controller: 您的控制器:

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

Back-end 后端

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM