【发布时间】:2016-11-11 11:54:05
【问题描述】:
我是 MEAN 堆栈的新手。 我正在使用 jwt 来验证 api 端点 '/api/candidates'
在客户端/angular js 服务中,我有以下条目
resumeFactory.get = function(candidateName) {
return $http.get('/api/candidates', {
headers: {
Authorization: 'Bearer '+ authenticationService.getToken()
});
};
在服务器端我有:
app.get('/api/candidates', auth, function (req, res) {
if (!req.payload._id) {
//user not logged in
console.log(req);
res.status(401).json({"message": "no user logged in from candidates"});
}
else {
Candidate.find({}, function (err, candidates) {
if (err) {
res.send(err);
}
res.json(candidates);
});
}
});
这很好用。即 'auth' 能够从标头中提取令牌
当我将所有内容更改为 post 而不是 get 时:
在客户端
$http.post()
在服务器端:
app.post()
我从 jwt 收到如下错误:
UnauthorizedError: No authorization token was found
对正在发生的事情有什么建议吗?我可以使用 get,但我想知道为什么 post 不起作用
【问题讨论】:
-
如果您真的在标头中发布该令牌,请检查开发工具。
-
@Nonemoticoner 我无法在开发工具中找到它。不确定如何在请求中查找标头
-
@Nonemoticoner - 我终于能够弄清楚如何检查标头 Networks->XHR
标签: angularjs node.js jwt mean express-jwt