TypeError:使用AJAX时,google.load不是函数
我正在使用Google图表(TermCloud)来显示一些数据。我可以将其作为页面上的一个静态功能很好地工作,但是当我尝试通过AJAX加载图表及其资产时,它似乎一直抛出错误:
'TypeError: google.load is not a function'
以下是我的AJAX函数:
$("li.contentpanel").click(function() {
$("#content-panel").show();
$('#content-panel').animate({
width: '540'
}, 500, function() {
var dataString = 'alert=1';
$.ajax({
type: "POST",
url: "<?php echo site_url($topicmaplink);?>",
data: dataString,
cache: false,
success: function(html){
$("#content-panel #inner").html(html);
}
});
});
这是名为:
的页面(JSAPI和TermCloud插件文件在此页顶部加载)
$(function() {
google.load("visualization", "1");
google.setOnLoadCallback(draw);
function draw() {
data = new google.visualization.DataTable();
data.addColumn('string', 'Label');
data.addColumn('number', 'Value');
data.addColumn('string', 'Link');
data.addRows(<?php echo sizeof($topics);?>);
<?php
$trans = array("ã" => "a", "³" => "3", "º" => "0", "â" => "a",
"¡" => ";", "'" => "", "
" => "",'"' => '');
shuffle($topics);
for($j=0;$j<sizeof($topics);$j++){
$nonforeignkeyword = strtr($topics[$j]['keyword'],$trans);
$totalnumber = $topics[$j]['occurrence'];
echo 'data.setValue('.$j.', 0, "'.trim($nonforeignkeyword).'");';
echo 'data.setValue('.$j.', 1, '.$totalnumber.');';
echo 'data.setValue('.$j.', 2, "'.$partlink.'/searchterm||'.trim(rawurlencode($nonforeignkeyword)).'");';
}
?>
var outputDiv = document.getElementById('cp-tmap');
var tc = new TermCloud(outputDiv);
tc.draw(data, null);
}
});
即使我从通过AJAX调用的页面中删除了JSAPI和TermCloud js文件,并将它们放在被调用的页面上,该页面似乎也会重定向到google.com并挂在空白页面上。
有人知道我哪里出错了吗?
提前感谢您的帮助
解决方案
为可能遇到相同问题的任何人解决了此问题。在google.load和setOnLoadCallback上删除,并在绘制DataTable后将它们放在各自的回调函数中:
<pre>
<code>
//google.load("visualization", "1");
//google.setOnLoadCallback(draw);
function draw() {
data = new google.visualization.DataTable();
data.addColumn('string', 'Label');
data.addColumn('number', 'Value');
data.addColumn('string', 'Link');
data.addRows(<?php echo sizeof($topics);?>);
<?php
$trans = array("ã" => "a", "³" => "3", "º" => "0", "â" => "a", "¡" => ";", "'" => "", "
" => "",'"' => '');
shuffle($topics);
for($j=0;$j<sizeof($topics);$j++){
$nonforeignkeyword = strtr($topics[$j]['keyword'],$trans);
$totalnumber = $topics[$j]['occurrence'];
echo 'data.setValue('.$j.', 0, "'.trim($nonforeignkeyword).'");';
echo 'data.setValue('.$j.', 1, '.$totalnumber.');';
echo 'data.setValue('.$j.', 2, "'.$partlink.'/authorname||'.trim(rawurlencode($nonforeignkeyword)).'");';
}
?>
var outputDiv = document.getElementById('cp-tmap');
var tc = new TermCloud(outputDiv);
tc.draw(data, null);
}
$(document).ready(function(){
setTimeout(function(){
google.load("visualization", "1",{"callback" : draw});
}, 100);
});
</code>
</pre>
相关文章