调用 Firebase Cloud Functions 时出现 CORS 错误

Roy*_*son 7 javascript http firebase google-cloud-functions

我有以下 hello world Firebase 云功能:

const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
    response.send("Hello from Firebase!");
});
Run Code Online (Sandbox Code Playgroud)

然后我从网页调用它,如下所示:

<script>
    // Initialize Firebase
    var config = { <config information copy-pasted from firebase console>
    };
    firebase.initializeApp(config);

    let helloWorld = firebase.functions().httpsCallable('helloWorld');
    console.log(helloWorld)
    helloWorld().then(function (result) {

    })
</script>
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

从源“null”获取(我的云函数 url)已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求中不存在“Access-Control-Allow-Origin”标头资源。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

我仍然可以直接访问 HTTP 端点。我究竟做错了什么?

在查看了以下内容后,我尝试了以下操作:在 Cloud Functions 中启用 CORS for Firebase

const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {

    return cors(req, res, () => {
        res.status(200).send('Hello World!')
    })
    //response.set('Access-Control-Allow-Origin', '*');
    //response.send("Hello from Firebase!");
});
Run Code Online (Sandbox Code Playgroud)

但是,当我访问 HTTP 端点时,出现错误:

错误:无法处理请求