通过AJAX从PHP传递JSON数据时出现Morris图表错误

时间:2015-09-26 03:51:32

标签: php json ajax morris.js

我使用以下代码通过json_encode提供数据库中的数据:

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

它返回此错误:

  

未捕获的TypeError:无法读取未定义的属性“x”

为什么会这样?

1 个答案:

答案 0 :(得分:0)

问题出在我如何用PHP构建我的JSON数据。这是代码:

 java.lang.IndexOutOfBoundsException: Invalid index 18, size is 5

   at package.packagename.FindingOpponentActivity$1.done(FindingOpponentActivity.java:190)
   at package.packagename.FindingOpponentActivity$1.done(FindingOpponentActivity.java:78)