Search code examples
javascriptjquerygetjson

getJSON not returning value


I am trying to fetch some data from my localhost url (/data) to another localhost url (/) but I'm not getting any data

...var x = (new Date()).getTime(), 
        y = 2.0;
        $.getJSON('/data', {}, function(data){
             value = data.tem_min;
        })
        series.addPoint([x, value], true, true);

Solution

  • getJSON is an async function so when you execute:

    series.addPoint([x, value], true, true);
    

    value is null because the function you passed to getJSON is not being executed until the http request is done.

    So you just need to use the value after it's set:

    $.getJSON('/data', {}, function(data){
        value = data.tem_min;
        series.addPoint([x, value], true, true);
    })