0

i am using this code in React app, it supposed to fire a notification to react native expo app. instead, i am getting this error in the browser's console.

i tried adding Access-Control-Allow-Origin, Access-Control-Allow-Methods...etc

let response = fetch('https://exp.host/--/api/v2/push/send', {
        method: 'POST',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          to: token,
          sound: 'default',
          title: 'Demo',
          body: 'Demo notificaiton'
        })
      });
Shareef Dweikat
  • 361
  • 2
  • 13

2 Answers2

3

Proxy any unknown requests to your API server in development, add a proxy field to your package.json, for example:

"proxy": "http://localhost:4000",

Read more

dadwic
  • 370
  • 3
  • 12
1

You either need to have an API that allows CORS, or the call has to be made to the same domain as the front-end. If you don't have access to the API server to allow CORS you need to add a proxy server. I've always relied on express for that, set up a local API that is the same domain as your front-end, then have the express API make the actual fetch call to the API server.

Jake Luby
  • 1,718
  • 5
  • 16
  • Hi, i am currently using firebase as my backend and hence cannot use express, how can i solve this ? – kd12345 Mar 13 '23 at 12:12
  • @kd12345 https://stackoverflow.com/questions/42755131/enabling-cors-in-cloud-functions-for-firebase – Jake Luby Apr 04 '23 at 17:06