score:2

Accepted answer

use onreadystatechange instead of onload, don't use request.responsetype = 'json'; and call open() and send() after declaring the onreadystatechange.

w3schools example

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.


Related Query

More Query from same tag