简体   繁体   English

Google setOnLoadCallback和Ajax

[英]Google setOnLoadCallback and Ajax

I am using ajax to hit a URL that serves Json through Node.js/Express. 我正在使用ajax来访问通过Node.js / Express为Json服务的URL。 The Json is returned fine, and I have tested it. Json的返回结果很好,我已经对其进行了测试。 The data it receives should be plotted in a graph using Google Chart API (Which works fine if the array is hard-coded). 应该使用Google Chart API将接收到的数据绘制在图形中(如果该数组是硬编码的,则可以很好地工作)。

I am recieving 4 unexpected 'Uncaught typeErrors' and would appreciate being pointed in the right direction. 我收到了4个意外的“未捕获的typeErrors”,希望将其指向正确的方向。

Your assistance is greatly appreciated, and welcomed! 非常感谢您的协助!

在此处输入图片说明

Here is the code in question (I think) 这是有问题的代码(我认为)

 <!-- <script>
            var nData;
        $(document).ready(function() {
            $.ajax({
                url: 'http://localhost:3000/renderedJson',
                type: 'GET',
                dataType: 'json',
                data: [],
                success:function(data){
                nData = data;
                console.log("success");
                console.log(nData);
                }
            })
            .done(function() {
                console.log("done");
                getData();
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });
            });
        </script>

Here is an example (with hard coded data, working! To see, comment the ajax request and uncomment the hardcoded variable) 这是一个示例(具有硬编码的数据,可以正常工作!要查看,请注释ajax请求并取消注释硬编码的变量)

Update - Here is my payload, working from the URL I setup through Express / Node (which makes a call to a MongoDB instance) 更新-这是我的有效负载,使用通过Express / Node(调用MongoDB实例)设置的URL进行工作

[{"_id":"559c27abe01e815c0f7b69d3","date":"2015-01-2","one":[{"a":9},{"b":8},{"c":7},{"d":6},{"e":5},{"f":4},{"g":3}],"two":[{"h":2},{"i":1}]},{"_id":"559c27abe01e815c0f7b69d2","date":"2015-01-1","one":[{"a":1},{"b":2},{"c":3},{"d":4},{"e":5},{"f":6},{"g":7}],"two":[{"h":8},{"i":9}]}]

Update After further debugging, I believe the following code is not firing off. 更新在进一步调试之后,我相信以下代码不会启动。

 google.setOnLoadCallback(function() {
console.log('loaded google 1');
drawChart(cols, rows);
console.log('loaded google 2');
});

The data is undefined with the getData() function. 使用getData()函数未定义数据。 The code you have currently posted is incomplete. 您当前发布的代码不完整。 Looking at your fiddle there is a line between getData and drawChart functions: getData(); 查看您的小提琴, getDatadrawChart函数之间有一条界线: getData();
that immediately calls your getData function before the ajax call has been completed. 在ajax调用完成之前立即调用getData函数。 Remove this line and it should work. 删除此行,它应该工作。

  • its very hard to see the errors on that picture. 很难看到那张照片上的错误。
  • the errors on that picture relate to the places where the errors are happening so just showing your ajax isnt enough to debug the problem (just for furture reference) 该图片上的错误与发生错误的位置有关,因此仅显示您的ajax不足以调试问题(仅供参考)
  • Its a good idea if things are undefined to check them within the function thats throwing the errors. 如果未定义事物以在抛出错误的函数中检查它们,则是一个好主意。 In this case if you log nData within getData it will probably output undefined - which can lead to where the problem has originated. 在这种情况下,如果将nData记录在getData ,则可能会输出undefined这可能会导致问题nData

edit 编辑

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    google.load('visualization', '1.1', {packages: ['line']});

then instead of 然后代替

google.load('visualization', '1.1', {packages: ['line']});
google.setOnLoadCallback(function() {
    drawChart(cols, rows);
});

jus have: 有:

drawChart(cols, rows);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM