【问题标题】:jQuery getScript returns an parse error exceptionjQuery getScript 返回解析错误异常
【发布时间】:2012-02-18 17:57:53
【问题描述】:

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

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

TypeError: 'undefined' is not a constructor'

但是,我不知道它是从哪里引用或从哪一行引用的,我认为它一定是试图在这个文件上执行地理编码器((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');
});

我尝试将 AJAX 设置更改为将 async 设置为 false,但这根本没有帮助。

【问题讨论】:

  • 你能显示../js/gomap.js的链接/源代码吗?错误位于该文件中,该文件与pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.js 不同。
  • 其实脚本是相等的。由于 NoScript,它在我的测试中不起作用。很抱歉给您带来不便,我希望我的工作解决方案可以减轻您的负担;)

标签: jquery ajax google-maps getscript


【解决方案1】:

该错误是由于 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');
});

【讨论】:

  • 嗯,我做了一个测试,在第一个 $.getScript 之前我输入了alert(typeof google),它返回了undefined,在 $.getScript 之后它返回了一个对象,这意味着它可以工作 - 不管是否它需要加载到头部?
  • @lolwut 仔细阅读我的回答。 Google Maps API 期望包含在头部中。您当前的代码动态加载 Google Maps API。这意味着您必须拦截document.write 方法(请参阅source code 以使代码正常工作。所以,我实际上提供了两种解决方案。使用其中一个,而不是两者
  • 啊,我明白了,感谢您的澄清 - 您的代码有效,感谢 :-)
  • 嗯,很抱歉打扰您。我在加载 goMap 时遇到错误,有时会加载,有时不会,您知道原因吗?
  • @lolwut 否。使用 Firebug、开发工具或 HttpFpx 检查网络活动,以确保请求脚本。还要检查document.write 是否等于function(){}{ [native code] },以确保脚本已加载或加载失败。您在哪个浏览器中遇到此问题?
【解决方案2】:

@lolwut 试试

$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function()
{
    alert(11);
    $.getScript('http://www.pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.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');
});

如果这行得通,那么你的相对路径 ../js/gomap.js 是错误的!

【讨论】:

  • 没有。我的路径我更正了,只是解析错误,如果是无效文件,会抛出 404 错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 2012-05-14
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多