我正在尝试对Openweathermap执行ajax调用,一旦函数离开ajax,我的变量就不会被更新。请有人解释一下为什么我的var tmp没有被更新。
var url1 = "http://api.openweathermap.org/data/2.5/find?lat=" + marker.position.lat()+"&lon="+marker.position.lng() + "&type=accurate&units=imperial&mode=json&APPID=8cbc2d4c3edf26435cf160f3cee969ed";
var tmp;
$.ajax({
type: 'get',
dataType: "jsonp",
url: url1,
async: false,
success:function (data) {
tmp =data.list[0].main.temp +"F " + data.list[0].weather[0].description;
console.log(tmp); //it logs correctly here
}
});
console.log(tmp); //it says it's undefined here发布于 2018-09-30 23:40:11
Ajax是异步的,不是同步的。
你可以在https://rowanmanning.com/posts/javascript-for-beginners-async/上读到
一些js解决方案
https://stackoverflow.com/questions/52582718
复制相似问题