服务器编码的JSON数据出现问题,无法绘制图表


Issue with JSON data encoded from the server, not able to plot the chart

我正在从服务器获取正确的JSON对象数组,并将其分配给系列。chart_cfd.series也在调试器中显示了正确的对象,但没有进行图表绘制。

这里可能有什么问题?我需要更改数组成员的类型吗?

注意:如果我在本地用javascript提供系列数据,图表会完美地填充(简单列类型)

Javascript:(我使用的是highcharts框架)

//图表对象中的series=[]

function requestCFDStatusData(){
  $.post(
     'ajax/cfdUtil/cfd_status.php',
 function(response){
                       chart_cfd.series = response;
                       });
}

PHP代码

<?php
header("Content-type: text/json");
$cfdData = array(
    array(
'name' => 'A',
'data' => array(49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4,22,34,23,11)),
    array(
'name' => 'B',
'data' => array(83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3,22,50,34,11)),
    array(
'name' => 'C',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,100,25,45,42)),
    array(
'name' => 'D',
'data' => array(42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1,32,33,46,54)),
    array(
'name' => 'E',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50)),
    array(
'name' => 'F',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50)),
    array(
'name' => 'G',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50)),
    array(
'name' => 'H',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50)),
    array(
'name' => 'I',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50)),
    array(
'name' => 'J',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50)),
    array(
'name' => 'K',
'data' => array(48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50))
);
$jsonEncoded = json_encode($cfdData);
echo $jsonEncoded;
?>

注意:如果我在javascript 中本地提供以下数据,则可以正常工作

series: [{
            name: 'A',
   data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4,22,34,23,11]
        }, {
            name: 'B',
   data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3,22,50,34,11]
        }, {
            name: 'C',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,100,25,45,42]
        }, {
            name: 'D',
   data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1,32,33,46,54]
        },{
            name: 'E',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50]
        },{
            name: 'F',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50]
        },{
            name: 'G',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50]
        },{
            name: 'H',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50]
        },{
            name: 'I',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50]
        },{
            name: 'J',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50]
        },{
            name: 'K',
   data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2,12,12,44,50]
        }]

在头中使用内容类型作为javascript

   header("Content-type: text/javascript");

然后使用jquery的getJSON方法作为

      $.getJSON('ajax/cfdUtil/cfd_status.php',function(response){
          chart_cfd.series = response;
                   });

您可以在php文件中使用application/json,也可以使用text/plain,然后使用$.parseJSON

function requestCFDStatusData(){
    $.post('ajax/cfdUtil/cfd_status.php', function(response){
        chart_cfd.series = $.parseJSON(response);
    });
}

这可能会对您有所帮助