我正在尝试从本地Web应用程序调用一个简单的helloWorld firebase云功能。当我调用函数但遇到CORS问题时,Firebase中的日志返回状态码200。我已经实现了CORS solution steps suggested by Firebase(请参见下面的index.js代码),但是在我的情况下这不起作用。我还能尝试解决这个问题吗?
当我在浏览器中调用端点时,它将返回:
{"headers":{"Access-Control-Allow-Origin":"*"},"body":{"message":"Hello world"}}
但是当我从我的应用程序中获取时:
fetch(endpoint, {
method: "GET",
mode: "no-cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
redirect: "follow",
referrer: "no-referrer",
}).then(response => {
console.log("response", response)
});
我得到一个不透明的响应:
我的index.js文件:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const express = require('express');
const cors = require('cors')({origin: true});
const app = express();
app.use(cors);
app.get('/', (req, res) => {
res.set('Access-Control-Allow-Origin', '*')
res.status(200).send({
headers: {'Access-Control-Allow-Origin': '*'},
body: { "message": "Hello world"},
});
});
exports.helloWorld = functions.https.onRequest(app);
答案 0 :(得分:2)
在提取时使用mode: cors
代替mode: no-cors
。