【发布时间】:2015-07-04 17:22:50
【问题描述】:
我想使用$.post 方法来发送和接收数据库表,并且我想将响应保存到我定义的数组中,但它不会将其保存在函数之外。如何解决?
var source = [];
$(function()
{
$.post("PostPath.php",
function(response){
var data = jQuery.parseJSON(response);
$.each(data, function( key, value) {
source.push(value);
alert(source); // here source array has values
})
})
})
alert(source); // but here source array is undefined
【问题讨论】:
-
你可以看看这个链接stackoverflow.com/questions/6477802/…我希望这能帮助你找到一些解决方案。
-
你必须明白 $.post 是一个异步调用。您的第二个警报(来源)在 $.post 之前执行。如果你尝试 alert(1), alert(2) 在那些地方你可以看到执行顺序。
标签: php jquery arrays post each