这是我得到的错误:
Access to XMLHttpRequest at has been blocked by CORS policy: Response to preflight request
doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是我的云功能:
...
app.use(cors({
origin: true,
}))
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.post('/submit', async (req, clientRes) => {
...
}
exports.app = functions.region('europe-west2').runWith({timeoutSeconds: 540, memory: '2GB'}).https.onRequest(app);
这是我从客户端提出的帖子请求:
axios.post('/submit', body, {headers: {
// 'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
}});
我尝试设置Access-Control-Allow-Origin: '*'
、Access-Control-Allow-Origin: mydomain.com
和Access-Control-Allow-Origin: cloudFunctionDomain.net
,但仍然不起作用。
非常感谢您的帮助。
根据[1],以这种方式导入似乎对许多人都有效:
const cors = require('cors')({origin: true});
如[1]中所述,您还可以从这里查看代码示例[2]。
[1] 为Firebase 启用云功能中的CORS
[2]https://github.com/firebase/functions-samples/