使用外部ajax数据PHP绘制图表

Chart with external ajax data PHP

本文关键字:绘制 PHP 数据 外部 ajax      更新时间:2023-09-26

我的网站上有一个图表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);

将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);

感谢John Fonseka!!!

我的代码如下:

$.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);

我希望它能帮助其他人。谢谢