JSON到jQuery图表

I have a PDO script that echos JSON from SQL table.

I am trying to display the JSON info in a jQuery chart with no success.

I get an error in chrome debugger which refers to the first jQuery code line below (var json).

Function Reference:

    Uncaught SyntaxError: Unexpected identifier
angular.js:11598 ReferenceError: Index is not defined
    at eval (eval at <anonymous> (http://localhost/assets/global/plugins/jquery.min.js:2:2622), <anonymous>:2:2)
    at eval (native)
    at http://localhost/assets/global/plugins/jquery.min.js:2:2622
    at Function.m.extend.globalEval (http://localhost/assets/global/plugins/jquery.min.js:2:2633)
    at m.fn.extend.domManip (http://localhost/assets/global/plugins/jquery.min.js:3:23159)
    at m.fn.extend.append (http://localhost/assets/global/plugins/jquery.min.js:3:20620)
    at null.<anonymous> (http://localhost/assets/global/plugins/jquery.min.js:3:22151)
    at m.access (http://localhost/assets/global/plugins/jquery.min.js:3:3399)
    at m.fn.extend.html (http://localhost/assets/global/plugins/jquery.min.js:3:21736)
    at compile (http://localhost/assets/global/plugins/angularjs/plugins/angular-ui-router.min.js:7:21866) <div ui-view="" class="fade-in-up ng-scope">(anonymous function) @ angular.js:11598$get @ angular.js:8548$ @ angular.js:8219v @ angular.js:7726g @ angular.js:7075(anonymous function) @ angular.js:6954k @ angular-ui-router.min.js:7(anonymous function) @ angular-ui-router.min.js:7$get.l.$broadcast @ angular.js:14707x.transitionTo.x.transition.I.then.x.transition.x.transition @ angular-ui-router.min.js:7(anonymous function) @ angular.js:13175$get.l.$eval @ angular.js:14388$get.l.$digest @ angular.js:14204(anonymous function) @ angular.js:14427e @ angular.js:4902(anonymous function) @ angular.js:5282

PHP:

<?php


$dbh = new PDO("mysql:host=localhost;dbname=test", "root", "");
$statement=$dbh->prepare("SELECT * FROM pdotable");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results, JSON_PRETTY_PRINT);

header('Content-type: application/json');

echo $json;
?>

jQuery:

var json = (function () {
                var json = null;
                $.ajax({
                    'async': false,
                    'global': false,
                    'url': 'pdo/phpinfo.php',
                    'dataType': "json",
                    'success': function (data) {
                        json = data;
                    }
                });
                return json;
            });

        initCharts: function() {

            if (Morris.EventEmitter) {
                // Use Morris.Area instead of Morris.Line
                dashboardMainChart = Morris.Area({
                    element: 'sales_statistics',
                    padding: 0,
                    behaveLikeLine: false,
                    gridEnabled: false,
                    gridLineColor: false,
                    axes: false,
                    fillOpacity: 1,
                    data:json,
                lineColors: ['#399a8c', '#92e9dc'],
                xkey: 'period',
                ykeys: ['sales', 'profit'],
                labels: ['Sales', 'Profit'],
                pointSize: 0,
                lineWidth: 0,
                hideHover: 'auto',
                resize: true
            });

        }
    },

What am I doing wrong?

dtxf759200
dtxf759200 谢谢m8..i仍然在你给我的行顶部得到一个意想不到的标识符。这是完整的jQuery文件(jsfiddle.net/cdxuku6L/1)
5 年多之前 回复
doumaikuang4202
doumaikuang4202 所以,我已经清理了你的代码了。看看这个,看看它是否适合你。
5 年多之前 回复
duanhuang2804
duanhuang2804 您是否注意到初始化varjson=function(...然后将其重新初始化为varjson=null?我可以建议您使用可以更“自解释”的变量名来帮助调试吗?
5 年多之前 回复
dsx5201
dsx5201 老实说,我对此很新,我想要做的是将php中的JSON信息转换为图表的“数据”
5 年多之前 回复
dongre6404
dongre6404 这段代码似乎有点不对劲。你期望initCharts方法属于json对象吗?或者json是IIFE吗?为什么你想让ajax同步?
5 年多之前 回复
duanpei4455
duanpei4455 意外的标识符指的是jQuery代码的第一行(“varjson”行)我想不出更多的代码要发布,因为它似乎与问题无关,我可能是错的,但这似乎是负责的代码。
5 年多之前 回复
doubi4435
doubi4435 将我们指向错误行#,函数参考并提供更多上下文。不要粘贴切碎的代码并期待答案!
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐