简体   繁体   English

jQuery JSONP AJAX请求有时会失败,并显示“ TypeError:无法调用未定义的方法'done'”

[英]jQuery JSONP AJAX requests sometimes fail with “TypeError: Cannot call method 'done' of undefined”

I'm trying to use jQuery JSONP AJAX requests in a bookmarklet and I'm finding that the requests work on some pages, but not on others. 我试图在小书签中使用jQuery JSONP AJAX请求,但发现该请求在某些页面上有效,而在另一些页面上无效。 Here's example code: 这是示例代码:

jQuery.ajax({
    url: "http://echo.jsontest.com/key/value/one/two",
    type: "GET",
    dataType: "jsonp",
    cache: false
}).done( function(data){
    console.log(data);
}).fail(function(jqXHR, textStatus, errorThrown){
    console.log("errorThrown: " + errorThrown);
});

If you go to the following Amazon page: 如果您转到以下亚马逊页面:

http://www.amazon.com/dp/B00336EUTK?psc=1 http://www.amazon.com/dp/B00336EUTK?psc=1

And execute the above code in your console, you'll find that it works. 并在控制台中执行上述代码,您会发现它起作用。 However, if you go to this Amazon page: 但是,如果您转到此亚马逊页面:

http://www.amazon.com/dp/B00E5UHSYW?psc=1 http://www.amazon.com/dp/B00E5UHSYW?psc=1

And execute the above code in your console, the following error appears: 并在控制台中执行以上代码,出现以下错误:

TypeError: Cannot call method 'done' of undefined

What is causing this problem and how can it be fixed? 是什么导致此问题,如何解决?

EDIT: One interesting thing I've noticed is that on pages where the AJAX requests works, the callback parameter always seems to look like this: 编辑:我注意到的一件有趣的事是,在AJAX请求工作的页面上,回调参数始终看起来像这样:

callback=jQuery1640586556919850409_1390439428297

On pages where it fails, it always seems to look like this: 在失败的页面上,它总是看起来像这样:

callback=jsonp1390439502398

I've tried this on a bunch of different Amazon pages and it seems like that behavior is consistent. 我已经在许多不同的Amazon页面上尝试过此操作,并且看起来这种行为是一致的。

Maybe it's just a coincidence, but I thought it might be worth pointing out. 也许这只是一个巧合,但我认为可能值得指出。

Different versions of jQuery 不同版本的jQuery

Believe it or not, those two pages are using different versions of jQuery. 信不信由你,这两个页面使用的是不同版本的jQuery。

You can determine this yourself by typing this into the console: $().jquery 您可以通过在控制台中键入以下内容来自己确定:$()。jquery

the first page is running v1.6.2 第一页正在运行v1.6.2

the second page is running v1.2.6 第二页正在运行v1.2.6

Looking at the jQuery docs for jQuery.ajax() ( here ), it looks like it was not added to jQuery until v1.5 看一下jQuery.ajax()的jQuery文档( 在此处 ),似乎直到v1.5才将其添加到jQuery中。

Hope that helps. 希望能有所帮助。

If you run a console.log(jQuery.ajax); 如果您运行console.log(jQuery.ajax); in console, you'll see that the second link you referenced does not contain a done function in the jQuery.ajax class (since they are two different versions). 在控制台中,您将看到引用的第二个链接在jQuery.ajax类中不包含完成函数(因为它们是两个不同的版本)。 You can do a version check and handle two different solutions depending on whether or not jQuery has that class method. 您可以进行版本检查并根据jQuery是否具有该类方法来处理两种不同的解决方案。 This works for both links you posted: 这适用于您发布的两个链接:

//get the jQuery version
var version = jQuery.fn.jquery,
    parts = version.split('.');

//piece together a version integer
version = parseInt(parts[0]+parts[1]+parts[2],10);

if (version >= 150) {
    //new method
    jQuery.ajax({
        url: "http://echo.jsontest.com/keyu/value/one/two",
        type: "GET",
        dataType: "jsonp",
        cache: false
    }).done( function(data){
        console.log(data);
    }).fail(function(jqXHR, textStatus, errorThrown){
        console.log("errorThrown: " + errorThrown);
    });
} else {
    jQuery.ajax({
        url: "http://echo.jsontest.com/keyu/value/one/two",
        type: "GET",  
        dataType: "jsonp",
        success: function(data){  
          console.log(data); 
        },  
        error: function(e){  
          console.log(e);
        }
    });  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法调用未定义的方法“ $ on” - TypeError: Cannot call method '$on' of undefined Uncaught TypeError:无法在jQuery jsonrpc客户端中调用未定义的方法“ setup” - Uncaught TypeError: Cannot call method 'setup' of undefined in jquery jsonrpc client 未捕获的TypeError:无法调用未定义的jQuery easydropdown的方法“ removeClass” - Uncaught TypeError: Cannot call method 'removeClass' of undefined jQuery easydropdown jQuery .each返回TypeError:无法调用未定义的方法'indexOf' - jQuery .each returning TypeError: Cannot call method 'indexOf' of undefined jquery 验证接受方法 - TypeError:无法读取未定义的属性“调用” - jquery validate accept method - TypeError: Cannot read property 'call' of undefined jQuery Uncaught TypeError:无法调用未定义的方法“ apply” - jQuery Uncaught TypeError: Cannot call method 'apply' of undefined jQuery单击事件错误:“ TypeError:无法在未定义的情况下调用方法'toString' - jQuery click event error: "TypeError: cannot call method 'toString' on undefined jQuery hoverIntent:'未捕获的TypeError:无法调用方法'apply'of undefined' - jQuery hoverIntent: 'Uncaught TypeError: Cannot call method 'apply' of undefined' jQuery TypeError“无法调用未定义的方法'toLowerCase'”(函数uaMatch) - jQuery TypeError “Cannot call method 'toLowerCase' of undefined” (function uaMatch) jQuery JSONP请求经常使用Internet Explorer失败 - jQuery JSONP requests fail frequently with Internet Explorer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM