0

I have a problem with fetch data. My friend creates Rest Api. There is my fun:

const AnyCors = `https://cors-anywhere.herokuapp.com/`;
const urlAllBus = `http://207.185.72.111:15430/stops?size=15`;
    fetchBusStop = () => {
  return new Promise((resolve, rejects) => {
    fetch(AnyCors+urlAllBus)
      .then((result) => {
        if (!result.ok) throw result.json();
        return result.json();
      })
      .then((result) => {
        console.log(result);
        resolve(result);
      })
      .catch((error) =>
        error.then((body) => {
          console.log('Bad', body);
          rejects(body);
        }),
      );
  });
};

I create an app with react-native. When I use only urlAllBus my virtual machine work fine. The problem is with a physical machine. When I try using my fun with urlAllbus in chrome I get a problem with CORS so I used AnyCors+urlAllBus and everything works fine. But in the virtual and physical machine there solutions not work. I don't know what I should do

Szot17
  • 21
  • 2

1 Answers1

0

You friend's API should accept CORS by adding a Access-Control-Allow-Origin: * header to its responses to allow any website to access it. They can also limit access to a specific site by setting the header to the base URL of such site, like Access-Control-Allow-Origin: http://example.com.

If the API is express-based, I hightly recommend the cors package for the job, as it makes it a single-line change.

Otherwise, tell them to give this MDN page a read for more information about CORS :)

Pedro Fracassi
  • 3,342
  • 2
  • 16
  • 33