json对象返回'undefined'值以及实际值

时间:2017-06-07 18:41:29

标签: javascript php jquery json

所以,我在这里看了类似的问题,但仍无法找到我的查询的答案。 出于某种原因,我似乎得到了“未定义的值以及我在json对象中的实际值。我从我的php文件中收到这些值.phDNA文件的代码如下:

$sql = "SELECT distinct rackID FROM rackusage where startTime like '%".$curdate."%'";
$res = mysql_query($sql);
while($r = mysql_fetch_assoc($res)){


        // Queries to get respective values to put inside my array

        $temp[] = array('id' => (int) $r['rackID']); 
        $temp[] = array('rackName' => (string) $rackname);
        $temp[] = array('uptime' => (int) $uptime); 
        $temp[] = array('downtime' => (int) $downtime); 
        $temp[] = array('utilization' => (int) $utilization);
    }   echo json_encode($temp);

输出字符串如下:

  

[{ “ID”:1},{ “rackName”: “XYZ”},{ “正常运行时间”:119},{ “停机时间”:0},{ “利用”:0},{ “ID” :2},{ “rackName”: “ABC”},{ “正常运行时间”:122},{ “停机时间”:0},{ “利用”:0},{ “ID”:3},{ “rackName” : “PQR”},{ “正常运行时间”:114},{ “停机时间”:0},{ “利用”:0}]

JS代码是:

        function workwithdate(curdate){
            $.ajax({
               type: "Post",
                url: 'senddata.php',
                data:{'selectDate': curdate},
                async: false,
                success: function(data){
                    if(data){
                        alert(data);
                        recData= $.parseJSON(data);
                        //recData = data;
                        console.log(recData);
                         alert(recData);
                         alert(recData.length);

                        $.each(recData, function(idx, la){
                          var someid = la.id;
                            console.log(someid);
                          var rName = la.rackName;
                            console.log(rName);
                          var rUptime = la.uptime;
                            console.log(rUptime);
                        });
       }
                }
            });
        }

我的输出如下:Console Output

TIA

1 个答案:

答案 0 :(得分:0)

每行需要一个关联数组,所以:

while($r = mysql_fetch_assoc($res)){
    $temp[] = array(
        'id' => (int) $r['rackID'], 
        'rackName' => (string) $rackname,
        'uptime' => (int) $uptime,
        'downtime' => (int) $downtime, 
        'utilization' => (int) $utilization
    );
}