数据值未以 JSON 格式显示


Data values not showing in json

我的脚本有问题,它没有显示图形和数据在论坛上搜索了所有内容,找不到答案为您提供我用于我的项目的代码

这是我的js代码:

$(function() {
$.getJSON('http://store.steamaccounts.me/test/jsonp.php?filename=test.json&callback=?', function(data) {
    // Create the chart
    window.chart = new Highcharts.StockChart({
        chart : {
            renderTo : 'container'
        },
        rangeSelector : {
            selected : 1
        },
        title : {
            text : 'test'
        },
        series : [{
            name : 'test',
            data : data,
            type : 'area',
            threshold : null,
            tooltip : {
                valueDecimals : 2
            },
            fillColor : {
                linearGradient : {
                    x1: 0, 
                    y1: 0, 
                    x2: 0, 
                    y2: 1
                },
                stops : [[0, Highcharts.getOptions().colors[0]], [1, 'rgba(0,0,0,0)']]
            }
        }]
    });
});

这是我的php代码:

<?php
$data = '{}'; // json string
if(array_key_exists('callback', $_GET)){
    header('Content-Type: text/javascript; charset=utf8');

    $callback = $_GET['callback'];
    echo $callback.'('.$data.');';
}else{
    // normal JSON string
    header('Content-Type: application/json; charset=utf8');
    echo $data;
}

我将不胜感激任何帮助感谢您的快速回复

从 JQuery 1.4 开始,它说如果你的 JSON 文件包含错误,那么方法 getJSON 就会静默失败。以防万一最好检查返回的 JSON 数据。

正如我所看到的,您的数据是空的,所以请确保您是否获得任何值。

我认为问题可能在于变量$data永远不会从空的 json 对象{}更改。您可以使用 php 函数 json_encode 创建一个 json 字符串。

json_encode接受数组作为输入,并将其转换为存储为字符串的 JSON 兼容对象。

然后,您可以回显字符串以返回到浏览器。