简体   繁体   中英

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);

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);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM