Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight respon

Access to fetch at ‘http://localhost:3000/books/231’ from origin ‘http://127.0.0.1:5500’ has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

// 设置允许跨域访问该服务
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Credentials', 'true');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header('Access-Control-Allow-Headers', 'mytoken');
  next();
});

在网上找了挺多最后解决方法是
加了一句
用于判断request来自ajax还是传统请求

 res.header("Access-Control-Allow-Headers", " Origin, X-Requested-With, Content-Type, Accept");

解决发送post请求时跨域无法访问

猜你喜欢

转载自blog.csdn.net/Sonnenlicht77/article/details/106226169