我正在尝试为将来的HTTP请求添加包含令牌的'Authorization'头文件。取回令牌似乎没问题,但是当发出get请求时,它会失败并显示未经授权的错误消息。检查请求头Authorization头后不请求块存在......
window.crUtil = /*window.crUtil ||*/ (function() {
// Angular Services
var $injector = angular.injector(['ng']);
var $http = $injector.get('$http');
// getting the CFF data
function get_data() {
\t \t getJWTAWS();
var url = '/AWS/getDATA/555';
\t \t \t \t console.log('AUTH header before call: ' + $http.defaults.headers.common.Authorization);
$http.get(url,httpHeader).then(function successCallback(response) {
var data = response.data;
var cff = initCff();
alert(data.itemId);
}, function errorCallback(response) {
initCff();
alert("Error while getting data ");
});
}
\t \t
function getJWTAWS() {
var httpConfig = {
cache: true,
params: {}
};
$http.get('/AWS/token', httpConfig).then(
function(response) {
if (response.data.accessToken) {
// add jwt token to auth header for all requests made by the $http service
$http.defaults.headers.common.Authorization = response.data.tokenType + ' ' + response.data.accessToken;
}
},
function(error) {
alert('jwt token could not be retrieved.');
}
);
}
})();
var result = util.get_data();
console.log ('called search function ' + result);
功能为gettoken()返回一个值,但我对这个主题是新我不能,如果十分确定我将标记添加到标题的方式是正确的。 您能否请您提供正确的方式在请求中包含标题。我也尝试将其添加到获取请求,如 $ http.get(URL,httpHeaders)... 但它也没有工作。