我指的是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
始终未定义。
答案 0 :(得分:0)
尝试
var bearerHeader = req.headers["Authorization"];
资本A而不是小A