我有下面的简单代码用于测试目的,但当我运行它时,我得到了CORS源错误。是因为我正在做POST调用吗?我已经尝试了一切,但没有运气
我在Cloud Console中测试了这个函数,它工作正常,所以FireBase函数没问题。但是我认为NUXT获取函数中有一些bug导致了这个错误。
任何帮助都将不胜感激。
Firebase函数node.js(index.js文件):
const admin = require("firebase-admin");
const functions = require("firebase-functions");
// Allow cross-origin requests
const cors = require("cors")({
origin: true,
});
admin.initializeApp();
exports.askQuestionIntent = functions.https.onRequest(async (req, res) => {
return cors(req, res, async () => {
console.log("yeeeeeee")
});
});
下面是我在前端调用的函数(Nuxt.js,存储文件):
export const actions = {
async askQuestion({ commit, state }, value) {
console.log(value);
const firebaseRest =
"https://us-central1-abc.cloudfunctions.net/askQuestionIntent";
let res = await fetch(firebaseRest, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(messagePrompts),
});
let data = await res.json();
console.log(data);
},
};
下面是控制台中的错误:
356594730026402389:1 Access to fetch at 'https://us-central1-abc.cloudfunctions.net/askQuestionIntent' from origin 'https://www.mysite.app' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
4ad7630.js:1
POST https://us-central1-abc.cloudfunctions.net/askQuestionIntent net::ERR_FAILED
(anonymous) @ 4ad7630.js:1
v @ 6d77cfc.js:2
(anonymous) @ 6d77cfc.js:2
(anonymous) @ 6d77cfc.js:2
r @ 6d77cfc.js:2
l @ 6d77cfc.js:2
(anonymous) @ 6d77cfc.js:2
(anonymous) @ 6d77cfc.js:2
askQuestion @ 4ad7630.js:1
(anonymous) @ 6d77cfc.js:2
m.dispatch @ 6d77cfc.js:2
dispatch @ 6d77cfc.js:2
askQuestion @ d3a30f6.js:1
ee @ 6d77cfc.js:2
n @ 6d77cfc.js:2
Dr.c._wrapper @ 6d77cfc.js:2
4ad7630.js:1
Uncaught (in promise) TypeError: Failed to fetch
1条答案
按热度按时间j5fpnvbx1#
找到OMG解决方案。您需要在云控制台设置中添加自己的调用。请参阅此处的帖子:https://github.com/firebase/functions-samples/issues/395#issuecomment-605025572