从jQuery.post AJAX调用返回数据?(Return data from jQuery.post AJAX call?)

编程入门 行业动态 更新时间:2024-10-27 14:33:40
从jQuery.post AJAX调用返回数据?(Return data from jQuery.post AJAX call?)

嗨,我正在调用此函数:

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()); 输出[],但第二次正确输出数组。 有任何想法吗?

提前致谢

Hi I'm calling this function:

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; }

like this:

$(document).ready(function(){ $('.caller').click(function() { console.log(getCoordenadas()); }); });

So when you click .caller it calls the function gets the data correctly populates the array, but console.log(getCoordenadas()); outputs [].

If I move the array declaration (var coordenadas = new Array();) from the function scope to make it global, when I click .caller for the first time console.log(getCoordenadas()); outputs [], but the second time it outputs the array correctly. Any ideas?

Thanks in advance

最满意答案

此函数异步工作。 AJAX帖子被触发,然后函数返回而不等待AJAX​​调用完成。 这就是为什么coordenadas数组是空的。

当你把它设为全局时,第一次它仍然是空的,并且第二次你尝试时,ajax返回并填充数组。 您应该重新构建代码以使用回调。 像这样的东西:

// definition function getCoordenadas(callback) { 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])); }); callback(coordenadas); }, 'json' ); } // usage $(document).ready(function(){ $('.caller').click(function() { getCoordenadas(function(coord) { console.log(coord); }) }); });

This function works asynchronously. AJAX post is fired and then function returns without waiting for AJAX call to complete. That's why coordenadas array is empty.

When you make it global, the first time it's still empty and by the second time you try, the ajax returned and populated the array. You should restructure your code to use a callback. Something like this:

// definition function getCoordenadas(callback) { 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])); }); callback(coordenadas); }, 'json' ); } // usage $(document).ready(function(){ $('.caller').click(function() { getCoordenadas(function(coord) { console.log(coord); }) }); });

更多推荐

本文发布于:2023-08-06 01:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   AJAX   post   jQuery   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!