I am getting the following error when running my project.
Failed to load https://us-centralx-xxx.cloudfunctions.net/xxx: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500.
After reading many SO post, I found the following solution, where I need to add the Access-Control-Allow-Origin
, Access-Control-Allow-Methods
, and Access-Control-Allow-Headers
const HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': 'http://localhost:3000/',
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'X-Requested-With,content-type'
};
However, the error still persist. How can i solve this ?
UPDATE
exports.uploadFile = functions.https.onRequest((req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,DELETE,HEAD,PUT,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.status(200).json({
message: req.body
});
});
nodejs
headers you need to allowhttp://localhost:3000 orgin
to get an access. – Eliasonres.setHeader()
as suggested by your questions and by other answers. – Yeaton