gpt4 book ai didi

node.js - axios 授权 header /CORS 错误

转载 作者:搜寻专家 更新时间:2023-10-31 23:39:47 25 4
gpt4 key购买 nike

我正在使用与 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');
});
}

错误:

enter image description here

当我使用 POSTMAN 时,我得到了正确的答案。

postman 输入:

enter image description here

附注我已经添加了:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com