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]);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?