0
我指this示例 我試圖通過$ localStorage從前端設置標頭。我已經在$ localStorage驗證了該令牌。無法從服務器端的請求獲取令牌
$httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) {
return {
'request': function (config) {
config.headers = config.headers || {};
if ($localStorage.token) {
config.headers.Authorization = 'Bearer ' + $localStorage.token;
}
return config;
},
'responseError': function(response) {
if(response.status === 401 || response.status === 403) {
$location.path('/signin');
}
return $q.reject(response);
}
};
}]);
在服務器端,我試圖獲取令牌,在我的路由中間件中進行身份驗證。
var api = express.Router();
api.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
next();
});
api.use(function(req, res, next) {
var bearerToken;
var bearerHeader = req.headers["authorization"];
}
bearerHeader
始終未定義。
有你在調試客戶端請求,看if'localStorage.token'不爲空,是否真發送到服務器? – pedrofb
是的,它是有效的併發送到服務器。 – NaaN