2016-11-18 5 views
0

jwtを使用してAPIにアクセスしようとしています。資格情報を使って投稿すると、サーバからid_tokenが得られます。私はそれを抽出しますが、Authorization headerの次のリクエストにトークンを追加しようとすると、undefinedというトークンが表示され、「JWT文字列は正確に2つのピリオドを含む必要があります。コンソールエラーが、JWT Authorizationヘッダーが定義されていません

私のコードは以下の通りである画像に示されている:あなたのトークンは2番目のリクエストの後に戻っているので起こりもちろん

angular.module('myApp', []).controller('myCtrl', function($scope, $http){ 
//$scope.tok = ''; 
$http({ 
    method : "POST", 
    url : "http://server.com/api/authenticate", 
    data: '{"username":"username","password":"password","rememberMe":true}', 
    headers:{"Content-Type": "application/json;charset=UTF-8", 
      } 
    }).then(
    function mySuccess(response){ 
     $scope.token = response.data.id_token; 
    }, function myError(response){ 
     console.log(response); 
    }); 

$http({ 
    method: "GET", 
    url: "http://server.com/api/account", 
    data: '', 
    headers:{"Authorization": "Bearer " + $scope.token, 
      "Content-Type": "application/json;charset=UTF-8"} 
}).then(
    function mySuccess(response){ 
     console.log(response); 
    }, function myError(response){ 
     console.log(response); 
    }); 

}); 

enter image description here

答えて

2

。オンザフライ あなたはこのようにそれを解決することができます:

angular.module('myApp', []).controller('myCtrl', function($scope, $http){ 
//$scope.tok = ''; 
$http({ 
    method : "POST", 
    url : "http://server.com/api/authenticate", 
    data: '{"username":"username","password":"password","rememberMe":true}', 
    headers:{"Content-Type": "application/json;charset=UTF-8", 
      } 
    }).then(
    function mySuccess(response){ 
     $scope.token = response.data.id_token; 
     $http({ 
      method: "GET", 
      url: "http://server.com/api/account", 
      data: '', 
      headers:{"Authorization": "Bearer " + $scope.token, 
       "Content-Type": "application/json;charset=UTF-8"} 
     }).then(
      function mySuccess(response){ 
       console.log(response); 
      }, function myError(response){ 
       console.log(response); 
      }); 

     }); 
    }, function myError(response){ 
     console.log(response); 
    }); 
+0

はあなたにヴァシリスありがとう;) –

+0

どういたしまして! –

関連する問題