简体   繁体   English

请求标头字段在飞行前响应中,Access-Control-Allow-Headers不允许进行授权。 (节点,反应,axios)

[英]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.

相关问题 Node JS - CORS - 请求 header 预检响应中的 Access-Control-Allow-Headers 不允许字段授权 - Node JS - CORS - Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response 允许使用restify的选项方法 - 请求头字段预检响应中的Access-Control-Allow-Headers不允许授权 - allowing options method with restify - Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response CORS 错误:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段授权 - CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response 在飞行前响应中,节点Js请求标头字段Access-Control-Allow-Headers不允许Access-Control-Allow-Origin - Node Js Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许 Angularjs 请求标头字段 Access-Control-Allow-Headers - Angularjs Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段身份验证 - Request header field authentication is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段内容类型 - Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response NodeJS + ExpressJS:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 - NodeJS + ExpressJS: Request header field not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段 ack 即使服务器已经允许它 - Request header field ack is not allowed by Access-Control-Allow-Headers in preflight response even the server already allowing it 请求已被 CORS 策略阻止请求 header 字段访问令牌在预检响应节点中被 Access-Control-Allow-Headers 不允许 - Request has been blocked by CORS policy Request header field access-token is not allowed by Access-Control-Allow-Headers in preflight response Node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM