你好,我打电话这个功能:从jQuery.post AJAX调用中返回数据?
function getCoordenadas()
{
var coordenadas = new Array();
$.post(
'<?=$this->baseUrl('user/parse-kml')?>',
{ kmlName: "esta_chica.kml"},
function(jsonCoord) {
jQuery.each(jsonCoord, function(i, val) {
var latlng = val.split(',');
coordenadas.push(new google.maps.LatLng(latlng[0], latlng[1]));
});
},
'json'
);
return coordenadas;
}
这样的:
$(document).ready(function(){
$('.caller').click(function() {
console.log(getCoordenadas());
});
});
因此,当你点击它.caller调用函数得到正确的数据填充数组,但执行console.log( getCoordenadas());输出[]。
如果我从函数范围移动数组声明(var coordenadas = new Array();)以使其成为全局函数,当我第一次单击.caller时console.log(getCoordenadas());输出[],但第二次输出数组正确。有任何想法吗?
在此先感谢
http://stackoverflow.com/questions/388436/jquery-ajax-return-value,http://stackoverflow.com/questions/2195161/how-to-return-an-array-from-jquery-ajax - 成功函数正确 – 2012-03-28 11:01:01
可能重复[如何返回数据到原来的调用者函数在Javascript?](http://stackoverflow.com/questions/1094716/how-does-one-return-data-to-原始呼叫者功能在JavaScript) – 2012-03-28 11:01:36