0

In my React app I am making a simple fetch request:

 useEffect(() => {
    fetch('http://localhost:3000/').then((response) => console.log('foobar', response));
  }, []);

This works fine. But when I try this with my real url like https://my-site.com/abc/token it doesn't work? When I try to fetch a different domain it doesn't work?

What could be the issue and how do I solve this?

meez
  • 3,783
  • 5
  • 37
  • 91

1 Answers1

0

Not many details in the post but I'll give it a go.

If you are receiving an error whilst in development similar to the one pictured below, then it means the server you are trying to contact does not allow the host you are developing on. This can be fixed either by either changing the security settings of your browser or installing a plugin that disables CORS.

However, if you are receiving this error in a non-local environment, then it means you should add the hostname of your frontend into the Access-Control-Allow-Origin on the serverside (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin).

Links for more details

  1. Disable CORS in Chrome + Safari and the plugin for Firefox - https://medium.com/swlh/avoiding-cors-errors-on-localhost-in-2020-5a656ed8cefa
  2. Plugin for Chrome - Why does my http://localhost CORS origin not work? CORS error
ezAZ
  • 11
  • 2
  • 1