Javascript $.ajax 抛出奇怪的“未捕获的类型错误:非法调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4279492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:42:15  来源:igfitidea点击:

$.ajax throwing weird "Uncaught TypeError: Illegal invocation"

javascriptajaxjquery

提问by Nathan MacInnes

I have some jQuery code which is throwing a really weird error. Google Chrome calls the error Uncaught TypeError: Illegal invocationand says it is thrown in c.extend.param.eof jquery-1.4.4.min.js line 144, but backtraces it to my $.ajax call, which looks like this:

我有一些 jQuery 代码,它抛出了一个非常奇怪的错误。谷歌浏览器调用错误Uncaught TypeError: Illegal invocation并说它是在c.extend.param.ejquery-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);
                }
            }
        }
    }
});

Another question on SO which looks a bit like this attributes it to using $without enclosing it in a jQuery function properly, but I'm pretty sure that's not my error this time, because I've been careful.

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

回答by Ivo Wetzel

Problems is here:

问题在这里:

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);

Changing this makes it work, reason it breaks?

改变这个让它工作,它为什么会坏?

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

encodeURIComponentdoes not like the window.locationobject as it only takes strings.

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

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

请参阅:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

回答by andres descalzo

try with jQuery.param with traditional parameter in true

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

documentation of param
modified

修改参数的文档