最初发布在这里:尝试通过 (jQuery)ajax 调用加载 Google 图表但对我的代码进行了一些修改,但我仍然无法让它正常工作。
我正在尝试编写一个轮询函数来加载结果并将其显示在同一页面中而不刷新。我正在使用谷歌图表 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"> </div></div>
目前,在我的 query.php 页面中,我只是让它吐出虚假的 JSON 数据:
<?php
if(isset($_POST['answer']))
{
echo "{\"yes\": 14,\"no\": 9}";
}
else
{
echo "{\"yes\": 9,\"no\": 14}";
}
?>
在我点击“是”按钮后,它所做的只是显示 ajaxloader.gif 图像。
我感到非常沮丧,我一生都无法弄清楚为什么会发生这种情况。任何帮助表示赞赏=)