亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaScript 在 JSON 請求上獲取

JavaScript 在 JSON 請求上獲取

一只萌萌小番薯 2023-09-07 18:23:54
我有一個基本的 Javascript Web 應用程序,它發送 JSON 請求并讀取從請求返回的數據。一周前,這段代碼運行良好,沒有錯誤。今天檢查的時候,每次都失敗。在此期間我沒有編輯任何內容。這是我的代碼的 JSON 請求部分。function Get(yourUrl){  var Httpreq = new XMLHttpRequest(); // a new request  Httpreq.open("GET",yourUrl,false);  Httpreq.send(null);  return Httpreq.responseText;}var json_obj = JSON.parse(Get('https://www.purpleair.com/json'));results = json_obj.results;控制臺上的完整讀數為:    [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.    GET https://www.purpleair.com/json 400 (Bad Request)誰能幫我弄清楚這筆交易是什么?自從我上次運行此命令以來,我發送 JSON 請求的網站似乎沒有發生變化,我只是完全困惑了。
查看完整描述

1 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

正如您在開發人員工具中看到的那樣 - 有些事情發生了明顯的變化:

https://img1.sycdn.imooc.com//64f9a4f80001710d09890479.jpg

關于已棄用的同步請求的警告只是警告 - 但我們可以使用異步請求來修復它...錯誤的請求...似乎可以通過添加到查詢字符串中的任何內容來修復...


function Get(yourUrl, yourHandler){

    var Httpreq = new XMLHttpRequest();

    Httpreq.open("GET",yourUrl,true); // true makes this request async

    Httpreq.onload = function(e) {

        var json_obj = JSON.parse(Httpreq.response);

        var results = json_obj.results;

        yourHandler(results);

    };

    Httpreq.send();

    return Httpreq;

}


var req = Get('https://www.purpleair.com/json?anything', function(results){

    // here you can place some code to do something with results

    // example below: alert the number of the results

    alert(results.length + ' result recieved');

});


查看完整回答
反對 回復 2023-09-07
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號