我有一个React + Node应用程序,我正在从React应用程序对节点层进行访存调用
fetch('/*', {
method: "GET"
})
.then(res => {
return res.json()
}).then(
actualRes => {
// SOME LOGIC
}
节点层正在调用某个第三方api,现在的问题是,当我尝试调用此第三方api时,我遇到了CORS问题。
确切错误:
从原点“ https://localhost:8443/”到“ <3RD PARTY URL>”(从“ https://localhost:8443”重定向)的访问已被CORS策略阻止:没有“ Access-Control-Allow-Origin”标头出现在请求的资源上。如果不透明的响应满足您的需求,请将请求的模式设置为“ no-cors”以在禁用CORS的情况下获取资源。
我尝试在节点层中添加cors标头
即
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'https://localhost:8443/*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});