我可以在Chrome DevTools中看到标题"x-auth-token“。
但是,报头没有显示在我的fetch响应中。请协助,以便我可以使用标题数据。
我使用NodeJS作为后端API,使用ReactJS作为我的前端。这些是我的文件。
NodeJS中间件- cors.js
module.exports = function enableCorsSupport(app) {
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type, x-auth-token");
res.header("Access-Control-Expose-Headers", "x-auth-token");
next();
})
}
NodeJS路由- users.js
router.post('/login', async (req, res) => {
// NOTE: code left out so post would be smaller
const token = user.generateAuthToken();
res.header('x-auth-token', token).send(_.pick(user, ['_id', 'firstName', 'email', 'isAdmin']));
})
ReactJS -我的获取请求
fetch('http://localhost:4000/api/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: this.props.email,
password: this.props.password
})
})
.then(res => {
console.log('res.headers', res.headers)
return res.json()
})
.then(data => {
console.log(data);
})
.catch((err) => {
console.log(err)
})
}
这是在我的Chrome控制台中,在我成功的fetch请求中的console.log。header响应中的header为空。请给我建议。仅供参考这是用户测试数据。
https://stackoverflow.com/questions/50779681
复制相似问题