Reputation: 476
I try to make HTTP POST request but I get this error
(everything is ok when I use same function on my angularjs project using __$http__
) I searched similar questions here but I think I am missing a basic point on my JS request code.
Access to XMLHttpRequest at 'https://us-central1-MYAPP.cloudfunctions.net/app/MYURL' from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Firebase side:
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({ origin: true }));
app.post('/MYURL', (req, res) => {
// my function goes here
var clientKey = req.body.clientKey;
return res.status(200).send(clientKey);
});
exports.app = functions.https.onRequest(app);
My request code:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("POST", "https://us-central1-MYAPP.cloudfunctions.net/app/MYURL", true);
xhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xhttp.send("clientKey=XXXXX");
Upvotes: 0
Views: 973
Reputation: 476
Since it has been a couple of days after my question, I want to share the result here. I contacted to Firebase Support and got replied that everything should be ok with my code. I know it is funny but it works without any CORS error right now. The fact is that I really did not change anything.
Upvotes: 1