【问题标题】:$.ajax throwing weird "Uncaught TypeError: Illegal invocation"$.ajax 抛出奇怪的“未捕获的类型错误:非法调用”
【发布时间】:2011-05-15 20:03:37
【问题描述】:

我有一些 jQuery 代码抛出了一个非常奇怪的错误。谷歌浏览器调用错误Uncaught TypeError: Illegal invocation 并说它是在 jquery-1.4.4.min.js 第 144 行的c.extend.param.e 中抛出的,但将其回溯到我的 $.ajax 调用,如下所示:

$.ajax({
   url: target,
   type: method,
   dataType: 'json',
   data: sendData,
   success: function(result) {
       if (result.redirect) {
           window.location = result.redirect;
       }
       else {
           for (var i in result) {
                if ($(i).size()) {
                    $(i).html(result.i);
                }
            }
        }
    }
});

另一个关于 SO 的问题看起来有点像这样,将其归因于使用 $ 而没有正确地将其包含在 jQuery 函数中,但我很确定这一次不是我的错误,因为我一直很小心。

【问题讨论】:

    标签: javascript ajax jquery


    【解决方案1】:

    问题就在这里:

    event.preventDefault();
    var data = $.extend({
        referrer: window.location, <-- window.location is an object,
                                       should be window.location.href
        href: $(this).attr('href')
    }, options.linkData);
    loadPage(options.linkUrl, options.linkMethod, data);
    

    改变它可以让它工作,为什么它会坏?

    <jQUery1.4.4 at line 6079>
    s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value)
    

    encodeURIComponent 不喜欢window.location 对象,因为它只需要strings。

    见:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

    【讨论】:

    • 没问题 :) 只花了 5 分钟(好吧,如果你知道你需要寻找什么)
    • event.preventDefault(); var data = $.extend({ referrer: window.location,
    • @ScottDavidTesler,有一个 jsfiddle 链接已被删除,因为它已经死了(抱歉)。我在问题中包含了我认为导致错误的部分,但 Ivo 找到了正确的部分。
    【解决方案2】:

    尝试使用 jQuery.param 和 true 中的传统参数

    documentation of param
    modified

    【讨论】:

    • 谢谢,这是另一个问题。这不是引发错误的人,但你为我省了很多麻烦。
    猜你喜欢
    • 2015-08-13
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多