[英]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我正在尝试访问 json 文件,但不断收到此错误
from origin 'chrome-search://local-ntp' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS 策略已阻止来自原点“chrome-search://local-ntp”:请求的资源上不存在“Access-Control-Allow-Origin”header。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。
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.当我尝试从打开浏览器、运行 devtools 并运行我编写的一些代码时......但是,如果我在浏览器中打开 json 文件并运行我的代码,那么它就可以工作了。
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?如何运行此代码,以便在我运行它时它可以工作,而无需打开 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. 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.