[英]Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response. (node, react, axios)
This is in my server.js: 这是在我的server.js中:
app.use(helmet())
app.options('*', cors())
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type,Cache-Control')
if (req.method === 'OPTIONS') {
res.sendStatus(200)
} else {
next()
}
})
I have a basic node/webpack/react situation. 我有一个基本的节点/ webpack /反应情况。 I am making my request in the client with axios.
我正在使用axios在客户端中提出请求。 I know that the request is solid, because I have tested it in isolation.
我知道该请求是可靠的,因为我已经对它进行了单独测试。 I have also tried every combination of
Access-Control-Allow-xxxxx
that I have found/can think of. 我也尝试过发现/想到的
Access-Control-Allow-xxxxx
每种组合。 It has to be something with this middleware. 这个中间件一定要有东西。 Any help/insights would be greatly appreciated!
任何帮助/见解将不胜感激!
I assume you are trying to make a api call from your client to the express/node server, yes? 我假设您正在尝试从客户端到Express / Node服务器进行api调用,是吗? Have you tried using a proxy?
您是否尝试过使用代理?
try setting the port of your server to something like localhost:5000. 尝试将服务器的端口设置为localhost:5000。 Then try
然后尝试
app.use(cors())
instead of 代替
app.options('*', cors())
then back in your client side in your React package.json simply add a proxy 然后回到您的React package.json中的客户端,只需添加一个代理
},
"proxy": "http://localhost:5000"
or whatever port your server is on. 或服务器所在的任何端口。
That should solve your issue. 那应该解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.