0

This is the error I'm getting :

Access to XMLHttpRequest at 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.

This is my cloud function:

...
app.use(cors({
  origin: true,
}))
app.use(express.json());
app.use(express.urlencoded({extended: false}));


app.post('/submit', async (req, clientRes) => {
...
}

exports.app = functions.region('europe-west2').runWith({timeoutSeconds: 540, memory: '2GB'}).https.onRequest(app);

This is the post request I'm making from the client side:

axios.post('/submit', body, {headers: {
      // 'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
    }});

I tried setting the Access-Control-Allow-Origin: '*', Access-Control-Allow-Origin: mydomain.com and also Access-Control-Allow-Origin: cloudFunctionDomain.net but it is still not working.

Help would be very much appreciated.

yashatreya
  • 782
  • 1
  • 9
  • 32

1 Answers1

-1

As per [1], importing this way seems to have worked for many:

const cors = require('cors')({origin: true});

As stated in [1], there are also code samples that you can take a look at from here[2].

[1]Enabling CORS in Cloud Functions for Firebase

[2]https://github.com/firebase/functions-samples/

eyoto
  • 1
  • 3