1👍
✅
Use onreadystatechange
instead of onload
, don’t use request.responseType = 'json';
and call open()
and send()
after declaring the onreadystatechange
.
var requestURL = 'http://api.holfuy.com/live/?s=101&m=JSON&tu=C&su=m/s'; //URL of the JSON data
var wData, hum;
var request = new XMLHttpRequest(); // create http request
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
wData = JSON.parse(request.responseText);
hum = wData.humidity;
console.log(wData);
console.log(hum);
}
}
request.open('GET', requestURL);
request.send(); // send the request
And by the way, you create the hum
variable to store the data you received inside the callback function scope, so the variable hum
does not exist when you are trying to create the chart. Create the variable before the ajax request and then populate it inside the callback, that way the variable will have value when you create your chart.
Source:stackexchange.com