简体   繁体   English

CORS 仅放置请求的预检问题

[英]CORS preflight issue on only put request

I am having a weird issue.我有一个奇怪的问题。 I get cors error for put request on browser:在浏览器上放置请求时出现 cors 错误:

Access to fetch at 'http://localhost:3015/pathtest/api/v1/results/cc7637578fad1a6fcfb4249fbf000a13/load' from origin 'https://localhost:9444' has been blocked by CORS policy: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.

And here is my code to enable cors in backend(nodejs).这是我在后端(nodejs)中启用 cors 的代码。 All the other api requests have been fixed with the below code and only put still returns cors issue:所有其他 api 请求已使用以下代码修复,仅 put 仍然返回 cors 问题:

app.use(function(req, res, next) {
  // res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  // res.header('Access-Control-Allow-Methods', 'PATCH, PUT, POST, GET, DELETE, OPTIONS');
  //     // allow preflight
  //     if (req.method === 'OPTIONS') {
  //       res.send(200);
  //   } else {
  //       next();
  //   }

  res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// allow preflight
if (req.method === 'OPTIONS') {
    res.send(200);
} else {
    next();
}
});

Am I missing anything?我错过了什么吗?

**Update **更新

在此处输入图像描述

From both of our investigation through comments, we've figured out that this was the cause of your problem...从我们通过评论进行的调查中,我们发现这是您的问题的原因......

The screenshot you show illustrates that the middleware code you show in your question is not handling that request.您显示的屏幕截图说明您在问题中显示的中间件代码未处理该请求。 That apparently means you have some other middleware before it that is taking over the request and that one is not doing what you want so it's getting in the way of the middleware you do want to handle the request.这显然意味着您在它之前有一些其他中间件正在接管请求,并且那个中间件没有做您想做的事情,所以它妨碍了您想要处理请求的中间件。

If, in your search for a solution, you've put something like app.use(cors()) (or some other code that attempts to handle CORs requests) into your code and that's before your own custom middleware, then it getting in the way of your own middleware.如果在您寻找解决方案的过程中,您将app.use(cors()) (或其他一些尝试处理 CORs 请求的代码)之类的东西放入您的代码中,并且在您自己的自定义中间件之前,那么它就会进入你自己的中间件的方式。 Remove that other CORS-related middleware so your custom middleware can get unimpeded access to the incoming requests and can do its job.删除其他与 CORS 相关的中间件,以便您的自定义中间件可以不受阻碍地访问传入的请求并完成其工作。

Remember that middleware and other request handlers are run in the order they were registered and if a given handler sends a response and does not call next() , then no more request handlers/middleware in the chain will get to act on that request.请记住,中间件和其他请求处理程序按照它们注册的顺序运行,如果给定的处理程序发送响应并且不调用next() ,那么链中没有更多的请求处理程序/中间件将对该请求采取行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM