0
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在我移动到下一行代码之前分配。基本上如何等到上面执行。

4

1 回答 1

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;
    }
})
于 2013-09-07T15:20:39.543 回答