简体   繁体   English

试图通过jQuery ajax调用加载Google图表

[英]Trying to load Google charts through a jQuery ajax call

Originally posted this here: Trying to load Google charts through a (jQuery)ajax call but have modified my code a bit but I still can't get it to work properly. 最初发布在这里: 尝试通过(jQuery)ajax调用加载谷歌图表,但已经修改了我的代码但我仍然​​无法让它正常工作。

I am trying to write a poll function that loads the results and displays it in the same page without refreshing. 我正在尝试编写一个轮询函数来加载结果并将其显示在同一页面中而不刷新。 I am using google charts api and jquery ajax. 我正在使用谷歌图表api和jquery ajax。

main page I have this: 主页我有这个:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load('visualization', '1.0', {'packages':['corechart']}); // Load the Visualization API and the piechart package.
    google.setOnLoadCallback(function(){  $("#poll_yes").removeAttr('disabled'); });

    function drawChart(rows) 
    {
        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Answers');
        data.addColumn('number', 'Number');
        data.addRows(rows);


        // Set chart options
        var options = 
        {
            'title'             :'Do you Like my poll?',
        };

        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);              
    }       

    jQuery(document).ready(function() {
        $.ajaxSetup ({  
            cache: false  
        }); 
        var ajax_load = "<img src='images/loading.gif' alt='loading...' />";  

        $("#poll_yes").click(function(){
            $("#result").html(ajax_load); 
            $.post(
                "query.php",  
                {answer: "yes", poll_id: 5},  
                function(response){ 
                    drawChart(response);
                }
            );
        });                 
    }); 
</script>
<input type="submit" value="yes" disabled="disabled" id="poll_yes"/>
<div id="result"><div id="chart_div">&nbsp;</div></div>

At the momemnt in my query.php page I just have it spitting out fake JSON data: 在我的query.php页面中的momemnt,我只是吐出假的JSON数据:

<?php 

if(isset($_POST['answer']))
{
    echo "{\"yes\": 14,\"no\": 9}";
}
else
{
    echo "{\"yes\": 9,\"no\": 14}";
}
?>

After I hit the 'yes' button all it does is show the ajaxloader.gif image. 点击“是”按钮后,它只显示ajaxloader.gif图像。

I'm feeling very frustrated and cannot for the life of me figure out why this is happening. 我感到很沮丧,不能为我的生活弄清楚为什么会这样。 Any help is appreciated =) 任何帮助表示赞赏=)

Your original code: 你原来的代码:

google.load('visualization', '1.0', {'packages':['corechart']}); // Load the Visualization API and the piechart package.

add one more parameter and it should be OK: 添加一个参数,它应该没问题:

google.load('visualization', '1.0', {'packages':['corechart'], **"callback": drawChart**});

First i would check if the the drawChart function behave correctly, next try updating your code to this 首先,我将检查drawChart函数是否正常运行,然后尝试将代码更新为此

$.post(
       "query.php",  
        {answer: "yes", poll_id: 5},  
        function(response){ 
          console.log(response); // check what the response from the server is
          drawChart(response);
        },
        'json' // the expected response data type
);

The response appears to have something to do with the type of data that is suppose to be an array.. 响应似乎与假定为数组的数据类型有关。

Uncaught Error: Argument given to addRows must be either a number or an array format+en,default,corechart.I.js:152 L.addRows format+en,default,corechart.I.js:152 drawChart

Test Data (test.php)... {"yes": 9,"no": 14} 测试数据(test.php)...... {“是”:9,“否”:14}

google.load('visualization', '1.0', {'packages':['corechart'], 'callback': drawChart}); // Load the Visualization API and the piechart package.
google.setOnLoadCallback(function(){  $("#poll_yes").removeAttr('disabled'); });

function drawChart(rows) 
{
    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Answers');
    data.addColumn('number', 'Number');
    data.addRows(rows);


    // Set chart options
    var options = 
    {
        'title'             :'Do you Like my poll?',
    };

    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);              
}       

jQuery(document).ready(function() {
    $.ajaxSetup ({  
        cache: false  
    }); 
    var ajax_load = "<img src='images.jpg' alt='loading...' />";  

    $("#poll_yes").click(function(){
        $("#result").html(ajax_load); 
        $.post(
               "test.php",  
                {answer: "yes", poll_id: 5},  
                function(response){ 
                  console.log(response); // check what the response from the server is
                  drawChart(response);
                },
                'json' // the expected response data type
        );
    });                 
}); 

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

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