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);
});
});
はあなたにヴァシリスありがとう;) –
どういたしまして! –