2016-08-03 45 views
0

在第一步中,我让谁returs与外地一个JSON一个$ HTTP POST请求“ACCESS_TOKEN”:在angularJS我想插入一个令牌,到http头

angular  
    .module('obparticularesmx') 
    .controller('inicio2', inicio2); 

    function inicio2($scope, $rootScope, $state, $http){ 

     $http({ 
      method:'POST', 
      url:'https://example.token-services.com/token', 
      data : { 
       client_id: "1561a116-7bde-4967-8471-8d6fb62511fa" 
      }, 
      headers: { 
      'Content-Type': 'application/x­www­urlencoded;' 
      } 
     }).success(function(data) { 

     $scope.token = data.access_token; 
     }); 
    } 

然后,我用的是工厂为了把该令牌到页眉:

.factory('httpRequestInterceptor', function ($rootScope) { 
     return { 
     request: function (config) { 
      config.headers = {'Authorization':'Bearer ' + $rootScope.token} 
     return config; 
    } 
    }; 
}) 

但是,当我发送的授权条目,我看到一个未定义的值:

Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Encoding gzip, deflate, br 
Accept-Language en-US,en;q=0.5 
**Authorization** Bearer undefined 
Host particulares-gw-obparticularesmx dev.appls.cto2.paas.gsnetcloud.com 
Origin http://localhost:3000 
Referer http://localhost:3000/ 
User-Agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0 

什么是解决方案?

+0

只要一提起来,一旦他们刷新应用程序,他们将不得不再次登录,除非这不是问题。 – Rob

回答

0

这条线:

$scope.token = data.access_token;

需要是

$rootScope.token = data.access_token;

这是因为$scope可以读取由$rootScope分配的值($rootScope属性涓滴所有$scope多个),但$rootScope无法读取分配给个人$scope的值。