简体   繁体   English

从 devtools 运行代码以获取 json 时出现 CORS 错误,但如果我打开 json 然后运行代码,则不会出现错误

[英]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.

相关问题 运行 axios 获取请求时收到 400 错误代码? - Getting 400 error code when I run axios get request? 使用“dotnet new react -o my-app”中的默认代码时,为什么会出现 JSON.parse 错误? - Why do I get JSON.parse error when using default code from “dotnet new react -o my-app”? 当我尝试解析数据以从 JSON 获取值时出现错误 - Getting an error when I try to parse data to get values from JSON 为什么在运行我的代码时会收到错误“UnhandledPromiseRejectionWarning”? - Why am I getting error "UnhandledPromiseRejectionWarning" when running my code? 从Chrome中的控制台运行代码时获取错误的堆栈跟踪 - Getting stack trace for an error when running code from the console in Chrome jQuery json错误,当我想从Steam市场获取json时 - jquery json error, when i want to get json from steam market 使用JSON POST到Flask Server时出现405 GET错误 - Getting a 405 GET error when using json POST to Flask Server 无法获取json数据,出现Cors或回调错误 - can't get json data, giving Cors or callback error 运行此脚本时出现错误代码401,该怎么办? - I get the Error code 401 when I run this script, what can I do? 使用Fetch和Cors时,如何从JSON API获取属性? - How to get properties from json API when using fetch, cors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM