我正在使用与 axios 的 react 来获取一些信息。我正在向 auth 用户发送 JWT token ,我在控制台浏览器中收到响应
"Access to XMLHttpRequest at 'https://beer-tonight.herokuapp.com/beer/getBeerStatus' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response."
这是我的代码:
componentWillMount(){
let token = localStorage.getItem('token');
axios.get('https://beer-tonight.herokuapp.com/beer/getBeerStatus', {headers: {Authorization : 'Bearer ' + token}}).then(
result => {
console.log('yey');
});
}
错误:
当我使用 POSTMAN 时,我得到了正确的答案。
postman 输入:
附注我已经添加了:
app.use((req, res,next)=>{
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorisation');
next();
最佳答案
您已经编写了授权
:
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorisation');
但是 header 是 Authorization
,因为您已经在 axios 中使用过,请尝试更改它并查看它是否有效,
关于node.js - axios 授权 header /CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54031193/