【问题标题】:Chart with external ajax data PHP带有外部 ajax 数据 PHP 的图表
【发布时间】:2016-05-11 02:36:49
【问题描述】:

我的网站上有一个图表 js! 我无法从外部 PHP 页面获取抓取数据。有人可以帮我吗?

var result = [];

        $.ajax({
                url: 'x.php',
                dataType: 'text',
                async: false,
                success:  function(data) {
                        items = data
                }
        });

     result = items


        var dataProcessosAtivos = [
                result
        ];

原变量如下:

var dataProcessosAtivos = [
        [0, 4],
        [1, 8],
        [2, 0],
        [3, 0],
        [4, 0],
        [5, 0],
        [6, 0],
        [7, 0],
        [8, 0],
        [9, 0],
        [10, 0],
        [11, 0]
];

这是一个 x.php:

    $json = "[0,87],[1, 14],[2, 16],[3, 0],[4, 0],[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]";

    echo json_encode($json);

【问题讨论】:

  • 您是否测试了x.php 以确保它实际返回 JSON、标头正确(根据 jQuery)以及它返回的数组是否符合您的预期?
  • 这是一个 x.php --- $json = "[0,87],[1, 14],[2, 16],[3, 0],[4, 0] ,[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]"; echo json_encode($json);

标签: javascript php jquery charts


【解决方案1】:

将 php 文件内容更改为如下。如果它是 DOM 中的现有图表,请确保在设置结果后重新绘制图表。

// This tells the bellow content is JSON
header ('content-type: application/json');

// Actual PHP array
$json = [[0,87],[1, 14],[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]];

// Printing JSON string
echo json_encode($json);

【讨论】:

    【解决方案2】:

    感谢约翰·冯塞卡!!!

    我的代码如下所示:

    $.ajax({
            url: 'x.php',
            dataType: 'json',
            async: false,
            success:  function(data) {
                    items = data
            }
    });
    
    var dataProcessosAtivos = items;
    

    和x.php

    header ('content-type: application/json');
    $json = [[0,87],[1, 14],[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]];
    echo json_encode($json);
    

    我希望它可以帮助其他人。谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多