【问题标题】:Issue with JSON data encoded from the server, not able to plot the chart从服务器编码的 JSON 数据出现问题,无法绘制图表
【发布时间】:2012-07-14 19:39:34
【问题描述】:

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

        }]

【问题讨论】:

  • 你的问题是什么?因为你的问题是说每件事都正确无误......
  • 图表绘图未发生,已更新,谢谢

标签: php javascript jquery json highcharts


【解决方案1】:

在 header 中使用 content-type 作为 javascript

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

然后使用jquery的getJSON方法作为

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

【讨论】:

  • 我想我在 PHP 中的 JSON 字符串转换有一些问题,因为我使用的是非关联数组:(
【解决方案2】:

也许你应该在你的php文件中使用application/json或者你可以使用text/plain然后你可以使用$.parseJSON

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

This may help you.

【讨论】:

  • 响应已经是一个 JSON 对象,因为我正在 PHP 中为 JSON 设置标头
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多