[英]Getting a CORS Error when running code from devtools to get json, but get no error if I open json then run code
我正在尝试访问 json 文件,但不断收到此错误
CORS 策略已阻止来自原点“chrome-search://local-ntp”:请求的资源上不存在“Access-Control-Allow-Origin”header。 如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。
当我尝试从打开浏览器、运行 devtools 并运行我编写的一些代码时......但是,如果我在浏览器中打开 json 文件并运行我的代码,那么它就可以工作了。
如何运行此代码,以便在我运行它时它可以工作,而无需打开 json 文件本身然后运行它?
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.