我正在开发一个带有 React 前端和节点后端的 Web 应用程序。它们都分别在我的本地计算机上localhost:3000运行localhost:8080。是否可以使用 CORS 请求headers: { "Content-Type": "application/json" }?基于查看其他问题,它似乎是,但我仍然在控制台中收到错误:
Access to fetch at 'http://localhost:8080/chordSheets/0' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
我的代码是:反应组件
fetch(`http://localhost:8080/chordSheets/${id}`, {
method: 'POST',
credentials: 'include',
mode: 'cors',
body: data,
headers: {
"Content-Type": "application/json",
},
})
节点设置:
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", 'http://localhost:3000');
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Options");
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
req.login = promisify(req.login, req);
next();
});
如果我理解正确,这应该允许 CORS 请求,"Content-Type": "application/json" 但我不明白为什么控制台错误The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*'在设置为localhost:3000.
提前致谢。