TypeError:google.load在使用ajax时不是函数

时间:2012-08-23 11:06:42

标签: jquery ajax google-visualization 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", 
        "¡" => ";", "'" => "", "\n" => "",'"' => '');
    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并挂在空白页面上。 / p>

有谁知道我在哪里出错?

提前感谢您的帮助

3 个答案:

答案 0 :(得分:2)

为可能遇到同样问题的任何人解决此问题。删除了google.load和setOnLoadCallback,并在绘制数据表后将它们放在自己的回调函数中:

<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", "¡" => ";", "'" => "", "\n" => "",'"' => '');
    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> 

答案 1 :(得分:1)

You need to add this script tag into your index.html

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"> </script>

You can check this from google charts site.

Geo chart from google

答案 2 :(得分:0)

我遇到了类似的问题,我的绘图功能无法找到它正在寻找的元素。它似乎失败了:

document.getElementById('map-canvas');

我发现成为google.load("visualization", "1");的原因必须是调用document.write,这意味着当我尝试抓取元素时,DOM完全为空。

我通过将google.load放入index.html来修复它,因此在进入draw()函数之前加载了可视化