簡體   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

我正在嘗試訪問 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM