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