0

Hello here is my problem:

I have to get some data to an external api (http://example.com/api/ who doesn't support OPTIONS) from my react app running in local with webpack-dev-server.

let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
    if (xhr.readyState === 4) {
        try {
            if (xhr.status === 200) {
                let response = JSON.parse(xhr.responseText);
                success(response);
            } else {
                error(xhr.responseText);
            }
        }
        catch(err) {
            console.error(err);
            error(err);
        }
    }
};

The status is always at 0 but 200 on the network.

I'm completely lost, how can makes it work ?

thib_hrp
  • 1
  • 2
  • usually means no CORS headers - have you checked your browsers **developer** tools console? note: you can't "beat" CORS, you can "proxy" the request via your own webserver - that's the easiest way of avoiding CORS – Jaromanda X Dec 20 '17 at 14:21
  • @JaromandaX I checked the console I get Failed to load `https://example.com/api/availability: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.` Ok, so it's should be possible to make it works with the webpack-dev-server proxy. Thanks, I will check if I find a solution – thib_hrp Dec 20 '17 at 14:26
  • `should be possible to make it works with the webpack-dev-server proxy` - if you say so, good luck – Jaromanda X Dec 20 '17 at 14:37

0 Answers0