【发布时间】:2018-08-27 07:00:12
【问题描述】:
我正在尝试为我的 http 云函数启用 cors。但是,即使关注this example that uses cors,尝试fetch 时,它仍然给我错误:
对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
这是我的云功能:
const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
exports.foo = functions.https.onRequest((req, res) => {
return cors(req, res, () => {
let format = req.query.format;
if (!format) {
format = req.body.format;
}
//do stuff
});
});
这是我获取的方式:
fetch("https://us-central1-my-database.cloudfunctions.net/foo", {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
}).then((res) => {
// ...
}).catch((err) => {
// ...
});
为什么 cors 不起作用,我该如何解决?
【问题讨论】:
-
试着看看这个案例:(stackoverflow.com/questions/49178210/…),它非常相似,它有一个答案,解释了如何在 Firebase Cloud 功能上启用 CORS。
标签: javascript node.js firebase cors google-cloud-functions