简体   繁体   English

即使在允许所有来源之后 cors 错误 *

[英]cors error even after allowing all origins *

i have a post request on my at http://localhost:3000 and request resources from http://localhost:5500 even after allowing all origins it gives error.我在 http://localhost:3000 上有一个 post 请求,并从 http://localhost:5500 请求资源,即使在允许所有来源之后它也会出错。 I'm stuck on this for a few hours now please help.我现在坚持了几个小时,请帮忙。

i get this error我收到这个错误

Access to fetch at 'http://localhost:3000/upload' from origin 'http://localhost:5500' has been blocked
by CORS policy:
Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

this is where i'm setting my header这是我设置标题的地方

app.all("*", (req, res, next) => {
  // CORS headers
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Credentials", "true");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Key, Authorization , client-security-token"
  );
  next();
});

this is my fetch request这是我的提取请求

 const resp = await fetch("http://localhost:3000/upload", {
          method: "POST",
          headers: {
            Authorization: `Bearer ${localStorage.getItem("access_token")}`,
            accept: "application/json",
          },
          body: formData,
        });
        const data = await resp.json();
        console.log(data);

Try below.下面试试。

Added a condition to check if the request type is OPTIONS and then return response with status 200 .添加了一个条件来检查请求类型是否为OPTIONS ,然后返回状态为200响应。

app.all("*", (req, res, next) => {
  // CORS headers
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Credentials", "true");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Key, Authorization , client-security-token"
  );
  if (req.method === "OPTIONS") {
      return res.status(200).end();
  }
  
  next();
});

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

相关问题 即使在添加 CORS 配置后 S3 的 CORS 错误 - CORS error with S3 even after adding CORS configuration 即使在设置标头后也出现 CORS 错误 - Having CORS error even after setting up headers 即使在 React/Node/Passport 上为 Google 身份验证添加 CORS 选项后仍面临 CORS 错误 - Facing CORS error even after adding CORS options on React/Node/Passport for Google Authentication Cors 不适用于两个不同的来源 - Cors is not working for two different origins 即使在允许脚本访问 Google 表格后调用 getDataSourceFormula() 时授权错误仍然存在 - Authorization error still persists when calling getDataSourceFormula() even after allowing script to acces Google Sheets 即使我包含了所有标题,也无法删除 CORS 错误 - Can't remove CORS error even so I included all headers 即使我使用允许服务器上的所有来源,请求的资源上也不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource even if I use allow all origins on server 即使在启用快速 CORS 中间件之后,axios 请求上的 Access-Control-Allow-Origin 错误 - Access-Control-Allow-Origin error on axios request even after enabling express CORS middleware 即使在后端设置了 cors,如何修复 cors 错误 - How to fix cors error faced even with cors setup on the backend 允许域上的 CORS - Allowing CORS on a domain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM