2017-02-20 217 views
2

我正在開發代碼以從遠程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); 

端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

你能爲你提供HTML你可以重寫你的代碼是怎樣的?什麼是'data.message'? – t3mplar

回答

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); 
+0

變更給出: 「[{」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」]},{ 「名稱」: 「出發」, 「數據」:[ 「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」]}]「 – Danard

+0

但現在的方式。無內容顯示。 – Danard

+0

@Danard:我剛編輯,嘗試用'返回json_encode($數據,JSON_NUMERIC_CHECK);' –