Skip to content
Advertisement

Invoking Firebase Function from Firebase hosting code blocked by CORS

I’ve initialized a templated Firebase project with functions & hosting features.

I’ve uncommented the templated HTTP function:

JavaScript

And Also added the following code to the templated public/index.html file:

JavaScript

I’ve tried making this work with multiple configurations:

  1. Firebase emulator for hosting, invoking the deployed Firebase function.
  2. Firebase emulator for hosting, invoking the emulated function (Firebase functions emulator).
  3. The deployed hosting, invoked the deployed Firebase function.

All configurations yield the following:

JavaScript

I didn’t change anything in the templated, auto generated Firebase code without the things I stated.

What am I missing?

Advertisement

Answer

You are actually mixing up HTTP Cloud Functions and Callable Cloud Functions.

Your helloWorld Cloud Function code corresponds to an HTTP one but the code in your front-end (i.e. public/index.html) calls a Callable one.

You should call the helloWorld Cloud Function as a REST API, i.e. with fetch or Axios, for example.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement