我正在开发代码以从远程Ajax获取数据,但无法显示所需的数据。
然而,标签的名称已正确显示。
从远程开始,在PHP中我就是这样:
for ( $i = $start; $i <= $end; $i = $i + 86400 ) {
$SQDate = date( 'Y-m-d', $i );
$PMS_ci = $this->getPMS_ci_status($SQDate);
$PMS_co = $this->getPMS_co_status($SQDate);
if($PMS_ci["sum"] == "")
$PMS_ci["sum"] = "0";
if($PMS_co["sum"] == "")
$PMS_co["sum"] = "0";
$arrival[] = $PMS_ci["sum"];
$departure[]= $PMS_co["sum"];
}
$data[] = array("name" => "Arrival", "data" => join($arrival,","));
$data[] = array("name" => "Departure", "data" => join($departure,","));
return JSON_encode($data);
Side Javascript:
retour = JSON.parse(data.message);
Highcharts.chart('container', {
chart: {
type: 'spline'
},
.../...
,
series: [{name: retour[0].name ,data: retour[0].data},
{name: retour[1].name, data: retour[1].data}],
navigation: {
.../...
数据内容返回:
[
{
"name":"Arrival",
"data":"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,0,0,0,0,0,0,0,0,0,0,0]"
},
{
"name":"Departure",
"data":"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,0,0,0,0,0,0,0,0,0,0]"
}
]
在所有情况下,都不会显示所需的数据,但会正确显示名称。
我不知道为什么。我在这里读了一些线索没有成功。应该怎么做才能确保数据与标签一起显示?
答案 0 :(得分:0)
发件人强>
$data[] = array("name" => "Arrival", "data" => join($arrival,","));
^
No need of join, json_encode
takes care of it
要强>
$data[] = array("name" => "Arrival", "data" => $arrival);
你的json变成了
发件人强>
[{"name":"Arrival","data":"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,0,0,0,0,0,0,0,0,0,0,0]"},{"name":"Departure","data":"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,0,0,0,0,0,0,0,0,0,0]"}]
^
Should be array not string
要强>
[{"name":"Arrival","data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,0,0,0,0,0,0,0,0,0,0,0]},{"name":"Departure","data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,0,0,0,0,0,0,0,0,0,0]}]
您可以像下面那样重写代码
for ( $i = $start; $i <= $end; $i = $i + 86400 )
{
$SQDate = date( 'Y-m-d', $i );
$PMS_ci = $this->getPMS_ci_status($SQDate);
$PMS_co = $this->getPMS_co_status($SQDate);
$arrival[] = $PMS_ci["sum"]+0;
$departure[]= $PMS_co["sum"]+0;
}
$data[] = array("name" => "Arrival", "data" =>$arrival);
$data[] = array("name" => "Departure", "data" => $departure);
// Return to caller (since PHP 5.3.3)
// and from caller do echo
return json_encode($data,JSON_NUMERIC_CHECK);