简体   繁体   中英

Getting a CORS Error when running code from devtools to get json, but get no error if I open json then run code

I am trying to access a json file, but keep getting this error

from origin 'chrome-search://local-ntp' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

when I try from just opening up a browser, running devtools and running some code that I wrote... However, if I open up the json file in the browser and run my code then it works.

How do I run this code so that it works when ever I run it without having to open the json file itself then run it?

async function GetCovidData(){
    const covidData = await fetch(JSON_I_WANT);
    const resp = await covidData.json();
    console.log(resp.filter(f=>f.prname == "Ontario"));
}

GetCovidData();

I found an answer about using https://cors-anywhere.herokuapp.com along in the url where the JSON was and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM