var path_data = path+"?q=map/get_data/"+param1+'/' +param2+'/'+param3+'/' +param4+'/' +param5;
var path_map_center = path+"?q=map/get_center/"+param1;
$.getJSON(path_map_center, function(data) {
center_lati = data.lati;
center_longi = data.longi;
});
//如何确保在转移到下一行代码之前分配center_lati
center_longi
。基本上如何等到上面执行。
答案 0 :(得分:2)
在基于ajax的系统中,遵循异步开发是明智的,这意味着需要处理ajax调用返回的数据的所有代码必须添加到相应的回调方法
var path_data = path + "?q=map/get_data/" + param1 + '/' + param2 + '/' + param3 + '/' + param4 +'/' + param5;
var path_map_center = path + "?q=map/get_center/" + param1;
$.getJSON(path_map_center, function(data) {
var center_lati = data.lati;
var center_longi = data.longi;
//all code that need to work with center_lati and center_longi should be added here
});
// do no put any ode that need to work with center_lati and center_longi should be added here
如果您不打算使用异步模式,请使用async
选项
var path_data = path + "?q=map/get_data/" + param1 + '/' + param2 + '/' + param3 + '/' + param4 + '/' + param5;
var path_map_center = path + "?q=map/get_center/" + param1;
$.ajax({
url: path_map_center,
dataType: 'json',
type: 'GET',
async: false,
success: function (data) {
center_lati = data.lati;
center_longi = data.longi;
}
})