我在cors和headers方面遇到了一些问题。我有以下中间件:
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type,X-Access-Token,Authorization');
next();在那之后,我有另一个中间件来检查token:
const token = req.body.token || req.query.token || req.headers['x-access-token'];
if (token) {
jwt.verify(token, config.jwtKey, (err, decoded) => {
if(err) {
return res.json({success: false, errmsg: 'Wrong key'});
} else {
req.decoded = decoded;
next();
}
});
} else {
return res.status(403)
.send({
success: false,
message: "No token provided"
});
}但当我登录req.headers时:
{ host: 'localhost:4556',
connection: 'keep-alive',
'access-control-request-method': 'POST',
origin: 'http://localhost:4200',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
'access-control-request-headers': 'authorization,content-type,x-access-token',
accept: '*/*',
dnt: '1',
referer: 'http://localhost:4200/posts',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'sv,en-US;q=0.8,en;q=0.6' }除了access-control-request- headers之外,我的headers中没有X-access-token。只是名字而已。一定是出了什么问题,但当我用谷歌搜索的时候,我发现的只是Access-Control-Allow-Headers。
发布于 2017-08-16 00:01:14
您现在看到的是一个preflight OPTIONS request。可以由许多条件触发,其中之一是“设置非标准标头”(如X-access-token)。
浏览器不会发出POST请求(带有X-access-token头),直到服务器响应OPTIONS请求才允许它。
您需要排除来自令牌检查中间件的OPTIONS请求,这样您就不会发送403作为对印前检查的响应(这永远不会包含令牌)。
https://stackoverflow.com/questions/45696388
复制相似问题