代码之家  ›  专栏  ›  技术社区  ›  Nathan MacInnes

$.ajax抛出奇怪的“Uncaught TypeError:非法调用”

  •  14
  • Nathan MacInnes  · 技术社区  · 14 年前

    我有一些jQuery代码,它抛出了一个非常奇怪的错误。谷歌Chrome称之为错误 Uncaught TypeError: Illegal invocation 说是扔进去的 c.extend.param.e jquery-1.4.4.min.js第144行,但回溯到我的$.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);
                    }
                }
            }
        }
    });
    

    另一个看起来有点像的问题是 $ 没有将它正确地封装在jQuery函数中,但我很确定这不是我的错误,因为我很小心。

    2 回复  |  直到 12 年前
        1
  •  19
  •   Ivo Wetzel    14 年前

    问题在于:

    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

        2
  •  2
  •   andres descalzo    14 年前

    尝试使用jQuery.param,将传统参数设置为true

    documentation of param
    modified