As the title suggests, I have a web app that calls a Firebase cloud function, but it doesn't work because of a cross-origin policy. I've seen many solutions on this site, but none work. I tried an answer from another StackOverflow question but nothing worked at all.
code in index.html:
firebase
.functions()
.httpsCallable("addMessage")()
.then((res) => {
console.log(res);
});
code in index.js (the cloud function):
exports.addMessage = onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into Firestore using the Firebase Admin SDK.
const writeResult = await getFirestore().collection("messages").add({ original: original });
// Send back a message that we've successfully written the message
res.set("Access-Control-Allow-Origin", "*");
res.json({ result: `Message with ID: ${JSON.stringify(original)} added.` });
});
the error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://us-central1-mysecretsite.cloudfunctions.net/addMessage. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 403.
I'm very confused on how these solutions even worked for other people as this seems to be a client-side problem and nothing added to the cloud function should fix it. please help!