Javascript “未捕获的类型错误:无法使用 'in' 运算符在“中搜索 'length' 由 Datatables 插件和 jQuery 1.11.3 触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31149836/
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
"Uncaught TypeError: Cannot use 'in' operator to search for 'length' in " triggered by Datatables plugin and jQuery 1.11.3
提问by James
I'm using the jQuery Datatables plugin to enable pagination, sorting and searching with my tables. The elements are showing up but not working, and the pagination only sometimes shows up. In Chrome console I'm getting the error:
我正在使用 jQuery Datatables 插件来启用我的表格的分页、排序和搜索。元素显示但不起作用,分页只是有时显示。在 Chrome 控制台中,我收到错误消息:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in
I'm using Bootstrap alongside this plugin.
我正在使用 Bootstrap 和这个插件。
回答by Dhiraj
That error is because of the method isArraylike
in jQuery version 1.11.3. (only). The method looks like this
该错误是由isArraylike
jQuery 1.11.3 版中的方法引起的。(只要)。该方法看起来像这样
function isArraylike( obj ) {
// Support: iOS 8.2 (not reproducible in simulator)
// `in` check used to prevent JIT error (gh-2145)
// hasOwn isn't used here due to false negatives
// regarding Nodelist length in IE
var length = "length" in obj && obj.length, // <------ THIS IS THE CULPRIT
type = jQuery.type( obj );
.......
}
That version of jQuery was using "length" in object to get the length. (I do not know anything about it).
该版本的 jQuery 在对象中使用“长度”来获取长度。(我对此一无所知)。
But I do know that no other versions of jquery have that issue.
但我知道没有其他版本的 jquery 有这个问题。
The versions 1.11.3 and 2.1.4 (as James pointed out in the comments) have this issue.
版本 1.11.3 和 2.1.4(正如 James 在评论中指出的)有这个问题。
So the solution would be to just upgrade to the next version or at least use any other version apart from 1.11.3 or 2.1.4
因此,解决方案是升级到下一个版本或至少使用除 1.11.3 或 2.1.4 之外的任何其他版本
回答by Alex
I'm working on Ruby on Rails with gem jquery-datatables-rails.
我正在使用 gem jquery-datatables-rails 开发 Ruby on Rails。
I update the gem directly from the last commit on GitHub:
我直接从 GitHub 上的最后一次提交更新 gem:
gem 'jquery-datatables-rails', github: "rweng/jquery-datatables-rails", branch: "master"
This work for me, I suppose that that they will release soon a new version of the gem with this commit.
这对我有用,我想他们很快就会通过这次提交发布新版本的 gem。
回答by Benjamin Crouzier
Upgrading to DataTables to DataTables 1.10.7
or 1.10.8-dev
did not work for me (using jQuery 1.11.3
).
升级到 DataTables 到 DataTables1.10.7
或 1.10.8-dev
对我不起作用(使用 jQuery 1.11.3
)。
Downgrading to jQuery 1.11.2
did work (using DataTables 10.0.0
)
降级到 jQuery1.11.2
确实有效(使用 DataTables 10.0.0
)
回答by raghavsood33
No need to downgrade jQuery.
I solved the same error by using aoColumns
as
无需降级 jQuery。我使用aoColumns
as解决了同样的错误
$('#id').DataTable( {
data: [["A", "B"], ["a", "b"]],
'aoColumns': [
{ sWidth: "50%", bSearchable: false, bSortable: false },
{ sWidth: "50%", bSearchable: false, bSortable: false }
],
} );
I am using jQuery 2.1.4
and DataTables 1.10.9
我正在使用jQuery 2.1.4
和DataTables 1.10.9
回答by bgs264
Whilst unrelated to DataTables, I came across this "cannot use in operator to search for length" error. This was the main Google result for the error so just wanted to post my issue as a response in case it helps anyone else.
虽然与 DataTables 无关,但我遇到了这个“不能在运算符中使用来搜索长度”的错误。这是错误的主要谷歌结果,所以只想发布我的问题作为回应,以防它帮助其他人。
I had:
我有:
ApplicationIDs: $.map(".application-checkbox:checked", function (checkedApplicationCheckbox, i) {
I'd forgotten to wrap my selector with the $
so the fix was to make sure I was passing the actual jQuery elements as the first argument to map
and not just a string...
我忘记用 包装我的选择器,$
所以修复是确保我将实际的 jQuery 元素作为第一个参数传递给map
,而不仅仅是一个字符串......
ApplicationIDs: $.map($(".application-checkbox:checked"), function (checkedApplicationCheckbox, i) {
I'm almost embarrased to post this ;)
我几乎不好意思发布这个 ;)
Cheers
干杯
回答by LeHill
I fixed a similar issue by adding the json dataType like so:
我通过添加 json dataType 解决了类似的问题,如下所示:
$.ajax({
type: "POST",
url: "someUrl",
dataType: "json",
data: {
varname1 : "varvalue1",
varname2 : "varvalue2"
},
success: function (data) {
$.each(data, function (varname, varvalue){
...
});
}
});
And in my controller I had to use double quotes around any strings like so (note: they have to be escaped in java):
在我的控制器中,我必须在任何字符串周围使用双引号(注意:它们必须在 java 中转义):
@RequestMapping(value = "/someUrl", method=RequestMethod.POST)
@ResponseBody
public String getJsonData(@RequestBody String parameters) {
// parameters = varname1=varvalue1&varname2=varvalue2
String exampleData = "{\"somename1\":\"somevalue1\",\"somename2\":\"somevalue2\"}";
return exampleData;
}
回答by Vishal
I had this exact same issue but jQuery version was not the culprit for me. In my case, I was incorrectly serializing the form. The code ended up with this error was:
我有这个完全相同的问题,但 jQuery 版本不是我的罪魁祸首。就我而言,我错误地序列化了表单。最终出现此错误的代码是:
$('#form_name').serialize()
Whereas I should have used.
而我应该使用。
$('#form_name').serializeArray()
I did this and my issue was resolved.
我这样做了,我的问题得到了解决。
Just throwing out this little piece that I ignored. Could help someone out there.
只是把这个我忽略的小块扔掉了。可以帮助那里的人。
回答by RWC
With DataTables and calling a PHP-script with AJAX, be aware that you just must echo your arrayat the end. There is no need of encoding it first to a JSON object with json_encode.
使用 DataTables 并使用 AJAX 调用 PHP 脚本,请注意您必须在最后回显您的数组。无需先使用 json_encode 将其编码为 JSON 对象。
So
所以
header('Content-type:application/json;charset=utf-8');
echo $myArray // This will do. Do not use echo json_encode($myArray);
exit();
Otherwise you might end up with the dreadful error
否则你可能会遇到可怕的错误
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in
未捕获的类型错误:无法使用“in”运算符来搜索“length”
or this one
或者这个
Uncaught TypeError: Cannot read property 'length' of undefined
未捕获的类型错误:无法读取未定义的属性“长度”