尝试通过jQuery的AJAX调用加载谷歌排行榜 [英] Trying to load Google charts through a jQuery ajax call
问题描述
最初发布此位置:<一href="http://stackoverflow.com/questions/8163408/trying-to-load-google-charts-through-a-jqueryajax-call">Trying通过(jQuery的)AJAX调用加载谷歌的图表,但已经修改了我的codeA一点,但我仍然不能得到正常的工作。
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.
我想写一个民意调查功能加载的结果,并在同一个页面无刷新显示。我使用谷歌图表API和jQuery阿贾克斯。
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.
主网页我有这样的:
<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页的momemnt我只是有它吐出假JSON数据:
At the momemnt in my query.php page I just have it spitting out fake JSON data:
<?php
if(isset($_POST['answer']))
{
echo "{\"yes\": 14,\"no\": 9}";
}
else
{
echo "{\"yes\": 9,\"no\": 14}";
}
?>
在我打的是按钮,它的作用是显示ajaxloader.gif形象。
After I hit the 'yes' button all it does is show the ajaxloader.gif image.
我感觉非常沮丧,并不能为我的生命弄清楚为什么发生这种情况。任何帮助是AP preciated =)
I'm feeling very frustrated and cannot for the life of me figure out why this is happening. Any help is appreciated =)
推荐答案
您原来的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**});
这篇关于尝试通过jQuery的AJAX调用加载谷歌排行榜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!