如何将每个JSON行添加到全局jQuery变量中?

时间:2014-12-03 11:42:52

标签: javascript php jquery ajax json

我有以下jQuery代码

该函数是,我通过AJAX调用每次从数据库中检索10行。

当在AJAX回调上接收到数据时,响应的每一行都会附加到全局数组中。

这是我的代码:

<script type="text/javascript">
var oTable;
var outer_start_row = 0;
var outer_limit = 10;
var final_data = []; 
$(document).ready(function() {
    window.prettyPrint() && prettyPrint();
    $('#load').click(function()
    {
        load_data_in_datatable();
    });
});

function load_data_in_datatable()
{

        var v = $('#drp_v').val();
        var cnt = $('#drp_cnt').val();
        var ctg = $('#drp_ctg').val();
        var api = $('#drp_api').val();
        var nt = $('#drp_nt').val();
        alert('outside start_row :'+outer_start_row);
        $.post("ajax.php",
            {   'version':v,'category':ctg,
                'country':cnt,'network_id':nt,
                'api':api,'func':'show_datatable',
                'start_row':outer_start_row,'limit':outer_limit},
                        function(response)
                        {
                            var data = response.data;
                            var limits = response.limits;
                            outer_limit = limits.limit;
                            outer_start_row = limits.start_row;
                            alert('inside start_row :'+outer_start_row);

                            final_data.push(data);

                            for(var f = 0; f < final_data.length; f++) 
                                alert(final_data[f].name);
                            load_data_in_datatable();
                        },'json');

}
</script>

但问题是,当我使用此警报时(final_data [f] .name);它的警报未定义。

不知道什么是错的

这是我的php ajax功能

function show_datatable($version,$ctg,$cnt,$nt,$api,$start_row,$limit)
{

    $cnt_table = "aw_countries_".$version;
    $ctg_table = "aw_categories_".$version;
    $off_table = "aw_offers_".$version;
    $sizeof_ctg = count($ctg);
    $cond_ctg = " ( ";
    for($c = 0; $c < $sizeof_ctg ; $c++)
    {
        $cond_ctg = $cond_ctg." $ctg_table.category = '".$ctg[$c]."' ";
        if($c < intval($sizeof_ctg-1))
            $cond_ctg = $cond_ctg." OR ";
        else if($c == intval($sizeof_ctg-1))
            $cond_ctg = $cond_ctg." ) ";
    }
    $sizeof_cnt = count($cnt);
    $cond_cnt = " ( ";
    for($cn = 0; $cn < $sizeof_cnt ; $cn++)
    {
        $cond_cnt = $cond_cnt." $cnt_table.country = '".$cnt[$cn]."' ";
        if($cn < intval($sizeof_cnt-1))
            $cond_cnt = $cond_cnt." OR ";
        else if($cn == intval($sizeof_cnt-1))
            $cond_cnt = $cond_cnt." ) ";
    }
    $sizeof_nt = count($nt);
    $cond_nt = " ( ";
    for($n = 0; $n < $sizeof_nt ; $n++)
    {
        $cond_nt = $cond_nt." $off_table.network_id = '".$nt[$n]."' ";
        if($n < intval($sizeof_nt-1))
            $cond_nt = $cond_nt." OR ";
        else if($n == intval($sizeof_nt-1))
            $cond_nt = $cond_nt." ) ";
    }
    $sizeof_api = count($api);
    $cond_api = " ( ";
    for($a = 0; $a < $sizeof_api ; $a++)
    {
        $cond_api = $cond_api." $off_table.api_key = '".$api[$a]."' ";
        if($a < intval($sizeof_api-1))
            $cond_api = $cond_api." OR ";
        else if($a == intval($sizeof_api-1))
            $cond_api = $cond_api." ) ";
    }
    $output         = "";
    $sql = "SELECT $off_table.id, $off_table.name
            FROM $off_table,$cnt_table,$ctg_table
            WHERE  $off_table.id = $cnt_table.id
            AND $off_table.id = $ctg_table.id
            AND ".$cond_api."
            AND ".$cond_nt."
            AND ".$cond_cnt."
            AND ".$cond_ctg." LIMIT $start_row , $limit";
    $result = mysql_query($sql);
    $arr_result = array();
    while($row = mysql_fetch_assoc($result))
    {
        $arr_result[] = $row;
    }
    //$arr_result_enc = json_encode($arr_result);
    //echo $arr_result_enc;



    $arr_limit = array(
            'start_row' => intval($start_row + $limit),
            'limit' => $limit
    );
    //$arr_limit_enc = json_encode($arr_limit);
    //echo $arr_limit_enc;
    $result_json = array(
    'data'   => $arr_result,
    'limits' => $arr_limit,
);

echo json_encode($result_json);
}

编辑:

这里是JSON格式

{"data":[{"id":"11105","name":"Gummy Drop (iPhone, Free, ROW except CN, 72.3MB, w"}],"limits":{"start_row":1,"limit":"1"}}

1 个答案:

答案 0 :(得分:1)

你无法像这样一次性推送整个数据:

final_data.push(data);

因为data本身是一个数组,并且数组没有name属性,这就是为什么你得到你看到的错误。你需要遍历它并推送各个元素:

for (var x = 0; x < data.length; x++)
    final_data.push(data[x]);