2015-09-26 55 views
0
从PHP传递JSON数据时,莫里斯图表错误

我有我通过json_encode把下面的代码数据库中的数据:通过AJAX

$orderList = $this->Retrieve->retrieve_data('*'); 
$data = array(); 
foreach ($orderList as $order) { 
    array_push($data, array(
     'd'  => $order['createdDate'], 
     'sales' => $order['order_price'] 
    )); 
} 
echo json_encode(array('data' => $data)); 

这是输出:

{"data":[{"d":"2015-09-26","sales":"0.00"},{"d":"2015-09-26","sales":"200.00"},{"d":"2015-09-26","sales":"45.00"},{"d":"2015-09-26","sales":"1500.00"}]} 

而且这是我的Javascript代码:

$.ajax({ 
    url: baseURL + '/Memberinfo/getGraphicalActivity', 
    cache: false, 
    type: "POST", 
    data: {patientFK: $("#patientFK").val()}, 
    dataType: "json", 
    timeout:3000, 
    success : function (data) { 
     memberArea = new Morris.Line({ 
     element: 'line-chart-memberInfo', 
     data: data, 
     xkey: 'd', 
     ykeys: ['sales'], 
     labels: ['Sales'], 
     smooth: false, 
     parseTime:false, 
     resize: true 
     }); 
    }, 
    error : function (xmlHttpRequest, textStatus, errorThrown) { 
     alert("Error " + errorThrown); 
     if(textStatus==='timeout') 
      alert("request timed out"); 
    } 
}); 

它返回此错误:

Uncaught TypeError: Cannot read property 'x' of undefined

这是怎么发生的?

回答

0

问题出在我如何在PHP中构建我的JSON数据。代码如下:

foreach ($orderList as $order) { 
    $arr[] = array(
     'd'  => ''.$order['createdDate'].'', 
     'sales' => ''.$order['order_price'].'', 
    ); 
} 
echo json_encode($arr);