0

What else do I need to add? First it was the a no header error, now its a preflight error(whatever that is). I tried turning it off via a browser plug-in as that was recommended. Error:

index.html:1 Access to XMLHttpRequest at 'http://www.quandl.com/api/v3/datasets/WORLDBANK/' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

Request:

function getMetals() {
    const xhr = new XMLHttpRequest();
    const url = 'http://www.quandl.com/api/v3/datasets/WORLDBANK/'
    xhr.onload = function() {
        if(xhr.status === 200) {
            console.log('ok');
        }
        else {
            console.log('not ok');
        }
    }
    xhr.open('GET', url, true);
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.send();
}

getMetals();
Adag89
  • 161
  • 10
  • Server decide whether it allows CORS, not the client. It's probably server setting. – Konrad Jan 31 '20 at 00:34
  • @Konowy I've tried 2 different APIs now, and same error, but Pokeapi works fine. If its a public API shouldn't it allow CORS, isn't that the whole point of it being public? – Adag89 Jan 31 '20 at 00:36
  • 2
    Could be that if you're just viewing the file in your browser, i.e. your url in the browser starts with `file:///`, the browser does not send an `Origin` header which fails the server's specified CORS policy (which the error "...from origin 'null' has been blocked..." suggests). If you serve your `index.html` via some web server on your local host, or try running it in JSFiddle or CodePen or something like that, does it work? – cbr Jan 31 '20 at 00:40
  • @cubrr I'm not sure, I haven't learned node yet, ill try codepen, but yes, I am running it in my browser. I just finished really getting a grasp on Vanilla JS, and Node was next to learn, do I need to learn it before doing this project to make it easier? – Adag89 Jan 31 '20 at 00:43

0 Answers0