dputlf5431 2014-12-03 11:42
浏览 52

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

I have a following jQuery code

The function is, I run it to retrieve 10 row each time from the database via AJAX call.

When the data is received back on AJAX callback, each row of the response is appended to a global array.

Here's my code:

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

but the problem is, when i am using this alert(final_data[f].name); its alerting undefined.

Don't know whats going wrong

here's my php ajax function

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

EDIT:

Here's a JSON format

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

1条回答 默认 最新

  • dongza1708 2014-12-03 11:53
    关注

    You can't push the entire data in in one go like this:

    final_data.push(data);
    

    as data itself is an array, and arrays don't have a name property, which is why you're getting the error you see. You need to loop through it and push the individual elements:

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

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源