jQuery getScript返回一个解析错误异常

Mac*_*Mac 5 ajax jquery google-maps getscript

我正在尝试加载两个具有$.getScript获取Google Map脚本功能的脚本,然后在加载之后,我得到另一个脚本(goMap),可以轻松制作地图小程序.

但是,在加载时,第一个获取Google Map API的脚本是好的,然后第二个脚本返回一个解析错误并显示:

TypeError:'undefined'不是构造函数'

然而,我不知道它在哪里引用或哪一行,我认为它必须尝试在此文件上执行Geocoder(第一行之后(function($){:

http://www.pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.js

这是我的代码:

$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function()
{
    $.getScript('../js/gomap.js').done(function()
    {
            // this never gets called
            alert('gomap loaded');
    }).fail(function(jqxhr, settings, exception)
    {
        alert(exception); // this gets shown
    });
}).fail(function()
{
    alert('failed to load google maps');
});
Run Code Online (Sandbox Code Playgroud)

我试图改变AJAX设置来设置asyncfalse,但它并没有帮助的.

Rob*_*b W 4

该错误是由于 Google Maps API 期望使用<script src="http://maps.google.com/maps/api/js?sensor=true"></script>.

如果由于某种原因你无法做到这一点,那么仍然有希望。Google Maps API 不起作用,因为它用于document.write在页面中注入依赖项。要使代码正常工作,您可以覆盖本机document.write方法。

演示: http: //jsfiddle.net/ZmtMr/

var doc_write = document.write; // Remember original method;
document.write = function(s) {$(s).appendTo('body')};
$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function() {
    $.getScript('../js/gomap.js').done(function() {
        alert('gomap loaded');
        document.write = doc_write; // Restore method
    }).fail(function(jqxhr, settings, exception) {
        alert(exception); // this gets shown
        document.write = doc_write; // Restore method
    });
}).fail(function() {
    alert('failed to load google maps');
});
Run Code Online (Sandbox Code Playgroud)