jQuery变量结果" undefined"即使全球化

时间:2014-05-11 17:51:24

标签: javascript jquery variables scope global-variables

我希望将JSON对象中的值存储在全局变量中,以便稍后在我的代码中使用。显然这不起作用,即使它应该使country变量全局。

$.getJSON(reverseGeoRequestURL, function(reverseGeoResult){
    window.country = reverseGeoResult.results[0].locations[0].adminArea1;
});
console.log(window.country);

1 个答案:

答案 0 :(得分:1)

getJSON调用是异步的。将console.log调用放在回调中,它不会被定义。

相关问题