0
我正在與Nodejs和Angular JS一起使用JWT令牌。我發現$ httpProvider在每個請求上設置標題的方式。但標題不會被髮送。我的NodeJS應用程序在訪問該標題時未定義。AngularJS不發送授權或任何額外的鍵/值標頭
我的角碼
有關設置授權令牌 -
$httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) {
return {
'request': function (config) {
config.headers = config.headers || {};
if ($localStorage.token){
console.log($localStorage.token)
config.headers.Authorization = 'Bearer ' + $localStorage.token;
console.log(config.headers);
}
return config;
},
'responseError': function(response) {
if(response.status === 401 || response.status === 403) {
$location.path('#login');
}
return $q.reject(response);
}
};
}]);
這裏是我的HTTP請求
$scope.getSearcHistory = function(){
console.log($localStorage.token);
console.log($rootScope.getCurrentUser());
$http({
url: baseUrl+'userSearches/2',
method : 'GET',
headers: {
'key': '1234424252'
}
})
.success(function(data, status, headers, config){
console.log(data);
$scope.searches = data;
})
.error(function(data, status, headers, config){
console.log(status);
});
}
這是我的位指示的NodeJS
this.router.use(function(req, res, next){
console.log(req.headers);
var bearerToken;
var bearerHeader = req.headers.authorization;
console.log(bearerHeader)
if (typeof bearerHeader !== 'undefined') {
var bearer = bearerHeader.split(" ");
bearerToken = bearer[1];
req.token = bearerToken;
next();
} else {
res.send(403).json({"Authorization": "Not allowed"});
}
});
這裏是我的節點控制檯,看有沒有頭,我需要 -
{
host: 'localhost:8080',
connection: 'keep-alive',
'access-control-request-method': 'GET',
origin: 'http://localhost:440',
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'access-control-request-headers': 'authorization,key',
accept: '*/*',
referer: 'http://localhost:440/vokal/'}
問題是,當我在Express應用程序中獲取標題時,請檢查我的問題'access-control-request-headers:'authorization,key''但我的req.headers.authorization和req.headers.key都未定義。我的意思是如何訪問該值? – Shailendra2014
如果你在req.headers上打印,你會得到什麼樣的價值? –
我的問題中的最後一個代碼塊是'console.log(req.headers') – Shailendra2014