我希望将JSON对象中的值存储在全局变量中,以便稍后在我的代码中使用。显然这不起作用,即使它应该使country
变量全局。
$.getJSON(reverseGeoRequestURL, function(reverseGeoResult){
window.country = reverseGeoResult.results[0].locations[0].adminArea1;
});
console.log(window.country);
答案 0 :(得分:1)
getJSON
调用是异步的。将console.log
调用放在回调中,它不会被定义。