我有一些 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 函数中,但我很确定这次不是我的错误,因为我'一直很小心。
最佳答案
问题在这里:
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
关于javascript - $.ajax 抛出奇怪的 "Uncaught TypeError: Illegal invocation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279492/