![](/img/trans.png)
[英]Bad Request Error (400) in json while posting to restful api in javascript
[英]Javascript gets 'GET https://www.purpleair.com/json 400 (Bad Request)' on JSON request
我有一個基本的 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 請求的網站似乎沒有改變,我只是完全糊塗了。
正如您在開發人員工具中看到的那樣 - 一些事情顯然發生了變化:
關於不推薦使用的同步請求的警告只是警告 - 但我們可以用異步請求修復它......錯誤的請求......似乎可以通過添加到查詢字符串的任何內容來修復......
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'); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.