Shareef Dweikat
Shareef Dweikat

Reputation: 809

Problem with Post API: "has been blocked by CORS policy...."

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'
        })
      });

Upvotes: 0

Views: 2387

Answers (2)

Jake Luby
Jake Luby

Reputation: 1758

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.

Upvotes: 1

dadwic
dadwic

Reputation: 378

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

Upvotes: 3

Related Questions