繁体   English   中英

如何将parseJSON响应保存到数组

[英]How to save parseJSON responses to an array

我想使用$.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

您推送路径,而不是源代码:

var source = [];
$(function
{
  $.post("PostPath.php",
        function(response){ 
        $.each(response, function( key, value) {
            source.push(value);
        })
        console.log(source);
    }, 'json') 
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM