2015-09-26 58 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);