2017-09-24 35 views
0

根據我在檢查代碼時看到的錯誤,它必須採用這種http格式,所以我無法更改它。我想弄清楚如何控制檯登錄這整個API文件。使openweathermap api內容在控制檯中打印?

var xhr = new XMLHttpRequest(); 

xhr.open("GET", "https://api.openweathermap.org/data/2.5/weather?zip=92407,us&appid=MY KEY GOES HERE YES I KNOW I HAVE TO ADD THE KEY", false); 
xhr.send(); 

console.log(xhr.statusText); 
+0

是否要打印回覆?你可以在console.log中使用'xhr.responseText' – Niladri

+0

它是Json格式 – Niladri

+0

耶穌基督謝謝你! – devynl

回答

0

我假設您需要控制API響應。要做到這一點,你必須使用readystatechange方法。嘗試使用此代碼段

xhr.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
    console.log(this.responseText); 
    }