简体   繁体   English

getJSON 不返回值

[英]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我试图从我的 localhost url ( /data ) 获取一些数据到另一个 localhost url ( / ) 但我没有得到任何数据

...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: getJSON是一个异步函数,所以当你执行时:

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. value为 null,因为您传递给getJSON的函数在 http 请求完成之前不会执行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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