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

时间:2012-07-14 19:39:35

标签: php javascript jquery json highcharts

我从服务器获取正确的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]

        }]

2 个答案:

答案 0 :(得分:2)

在标题

中使用内容类型作为javascript
   header("Content-type: text/javascript");

然后使用jquery的getJSON方法作为

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

答案 1 :(得分:1)

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

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

This may help you