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

Trying to load Google charts through a jQuery ajax call

本文关键字:调用 加载 Google 图表 ajax jQuery      更新时间:2023-09-26

最初张贴在这里:试图通过(jQuery)ajax调用加载谷歌图表,但修改了我的代码一点,但我仍然不能让它正常工作。

我正试图编写一个民意调查函数,加载结果并在同一页面中显示它而不刷新。我使用谷歌图表api和jquery ajax。

主页我有这个:

<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>

目前在我的query.php页面中,我只是让它吐出虚假的JSON数据:

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

当我点击"是"按钮后,它所做的就是显示ajaxloader.gif图像。

我感到非常沮丧,我一辈子也弄不明白为什么会发生这种情况。如有任何帮助,不胜感激=)

您的原始代码:

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

再添加一个参数,应该就可以了:

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

首先我会检查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
);

响应似乎与假定为数组的数据类型有关。

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}

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