Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图通过jQuery ajax调用加载Google图表_Javascript_Jquery_Ajax_Google Visualization - Fatal编程技术网

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

Javascript 试图通过jQuery ajax调用加载Google图表,javascript,jquery,ajax,google-visualization,Javascript,Jquery,Ajax,Google Visualization,最初在这里发布:但是我已经修改了我的代码,但是我仍然不能让它正常工作 我正在尝试编写一个轮询函数,加载结果并在同一页面中显示,而不刷新。我正在使用GoogleChartsAPI和jQueryAjax 主页我有这个: <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load(

最初在这里发布:但是我已经修改了我的代码,但是我仍然不能让它正常工作

我正在尝试编写一个轮询函数,加载结果并在同一页面中显示,而不刷新。我正在使用GoogleChartsAPI和jQueryAjax

主页我有这个:

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

load('visualization','1.0',{'packages':['corechart']});//加载可视化API和piechart包。
setOnLoadCallback(函数(){$(“#poll_yes”).removeAttr('disabled');});
功能绘图图(行)
{
//创建数据表。
var data=new google.visualization.DataTable();
data.addColumn('string','Answers');
data.addColumn('number','number');
data.addRows(行);
//设置图表选项
变量选项=
{
“标题”:“你喜欢我的民意测验吗?”,
};
var chart=new google.visualization.PieChart(document.getElementById('chart_div');
图表绘制(数据、选项);
}       
jQuery(文档).ready(函数(){
$.ajaxSetup({
缓存:false
}); 
var ajax_load=“”;
$(“#poll_yes”)。单击(函数(){
$(“#结果”).html(ajax#u加载);
美元邮政(
“query.php”,
{回答:“是”,投票编号:5},
功能(响应){
图纸(响应);
}
);
});                 
}); 
在我的query.php页面中,我刚刚看到它吐出假JSON数据:

<?php 

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

在我点击“是”按钮后,它所做的就是显示ajaxloader.gif图像


我感到非常沮丧,我一辈子都不明白为什么会发生这种事。非常感谢您的帮助=)

首先,我会检查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
);
您的原始代码:

google.load('visualization', '1.0', {'packages':['corechart']}); // Load the Visualization API and the piechart package.
再添加一个参数,应该可以:

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

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

Uncaught错误:提供给addRows的参数必须是数字或数组格式+en,默认值,corechart.I.js:152
L.addRows格式+en,默认值,corechart.I.js:152
图纸

测试数据(Test.php)。。。{“是”:9,“否”:14}

google.load('visualization','1.0',{'packages':['corechart'],'callback':drawChart});//加载可视化API和piechart包。
setOnLoadCallback(函数(){$(“#poll_yes”).removeAttr('disabled');});
功能绘图图(行)
{
//创建数据表。
var data=new google.visualization.DataTable();
data.addColumn('string','Answers');
data.addColumn('number','number');
data.addRows(行);
//设置图表选项
变量选项=
{
“标题”:“你喜欢我的民意测验吗?”,
};
var chart=new google.visualization.PieChart(document.getElementById('chart_div');
图表绘制(数据、选项);
}       
jQuery(文档).ready(函数(){
$.ajaxSetup({
缓存:false
}); 
var ajax_load=“”;
$(“#poll_yes”)。单击(函数(){
$(“#结果”).html(ajax#u加载);
美元邮政(
“test.php”,
{回答:“是”,投票编号:5},
功能(响应){
console.log(response);//检查服务器的响应是什么
图纸(响应);
},
'json'//预期的响应数据类型
);
});                 
}); 

您是否有指向功能站点的链接,或者是否可以将代码发布到JSFIDLE?我只是想说声谢谢,我已经搜索了很长一段时间的答案,但通过setTimeout()找到了各种解决方法,而这一简单的更改确实很神奇。
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
        );
    });                 
});