跨域问题 OPTIONS 报404 It does not have HTTP ok status
node.js后端代码添加了res.setHeader('Access-Control-Allow-Origin', '*')等处理跨域问题,但是请求时OPTIONS请求还是返回 404, 同时控制台报错: It does not have HTTP ok status.请问该如何处理
Request URL: http://127.0.0.1:8000/api/user/login
Request Method: OPTIONS
Status Code: 404 Not Found
Remote Address: 127.0.0.1:8000
Referrer Policy: no-referrer-when-downgrade
Access to XMLHttpRequest at 'http://127.0.0.1:8000/api/user/login' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
node.js
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.setHeader('Access-Control-Allow-Origin', '*') // 允许跨域
res.setHeader('Access-Control-Allow-Credentials', true) // 是否允许发送Cookie,ture为运行
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, PUT, POST, DELETE')
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Access-Control-Allow-Headers, Access-Control-Request-Headers, Access-Control-Request-Method, Authorization, X-Requested-With, User-Agent, Referer, Origin"
)
res.setHeader("Access-Control-Max-Age", 1728000)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
res.setHeader('Access-Control-Allow-Origin', '*') // 允许跨域
res.setHeader('Access-Control-Allow-Credentials', true) // 是否允许发送Cookie,ture为运行
如果你跨域想带上凭证信息,那么Access-Control-Allow-Origin就不能设置为*,
可以设置为 req.origin (获取请求origin)