Google Api异步回调失败

Google Api Asynch Callback Failed

本文关键字:失败 回调 异步 Api Google      更新时间:2023-09-26

我得到一个请求的内容:

function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );
    xmlHttp.send( null );
    alert(xmlHttp.status);
    return xmlHttp.responseText;
}

内容被解析到我的新网站后:

$Test = httpGet("www.content.com");
document.getElementById("div_content").innerHTML = document.getElementById("div_content").innerHTML + $Test;

在此之后,我将使用谷歌api asynch与回调,但回调函数抛出一个错误

loadScriptFunc('https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&callback=loadScript');

错误是:window.loadScript is not a function

我已经用jquery $做过了。这对我很有效。

编辑:函数是这样加载的:

if(document.getElementById('map_canvas')){
            loadScriptFunc('https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&callback=loadScript');
          } 

函数本身看起来像:

function loadScriptFunc(url) 
{
   var head = document.getElementsByTagName('head')[0];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = url;
   head.appendChild(script);
}   

loadScript函数是这样的:

function loadScript() {
           var head = document.getElementsByTagName('head')[0];
           var script = document.createElement('script');
           script.type = 'text/javascript';
           script.src = 'http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js';
           head.appendChild(script);
           bLoaded = true;
           window.setTimeout("activateMap()", 100);
         }  

loadScript Functions属于我的map的模板文件。这个代码已经为我工作与jquery - $。获取函数相同的回调等等。但是我不允许使用jquery…

Edit2:我在网上找到了一些东西,我想我知道问题出在哪里了。

我已经成功地使用了jQuery.load(),因为它执行JavaScript代码块和资源,这些代码块和资源存在于获取的内容中。抱歉,没有演示!

我想我不能从XMLHttpRequest执行代码,有人知道另一个js方法从Url获得一些内容吗??

啊,我知道怎么回事了。这是谷歌地图告诉你它找不到loadScript()。

你告诉Google地图:maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&callback=loadScript

看看发生了什么;我告诉Google Maps调用initialize。

<style>
  #map_canvas {width: 400px; height: 400px;}
</style>
<div id="map_canvas"></div>
<script>
function initialize() {
  var my_position = new google.maps.LatLng(50.5, 4.5);
  var map = new google.maps.Map(document.getElementById('map_canvas'), {
    center: my_position,
    zoom: 7
  });
}
function loadScriptFunc(url) {
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = url;
  head.appendChild(script);
}
if(document.getElementById('map_canvas')) {
  loadScriptFunc('https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&callback=initialize');
}
</script>