类型错误:jquery 1.9.1 版本中的“in”操作数 obj 无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16602487/
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-26 17:27:37  来源:igfitidea点击:

TypeError: invalid 'in' operand obj in jquery 1.9.1 version

jquery

提问by user2392605

$.ajax({
    async: false,
    type: "POST",
    url: url+"module/listing/"+projectId,
    data: "ajax=true",
    success: function(response) {
        $.each(response, function(key, val) {
        alert(val.id);
        });
    }
});

This is my code. Ajax success I am getting JSON response. Response is in array format. And I want to alert the response. But getting error "TypeError: invalid 'in' operand obj"for JQuery 1.9.1 version.

这是我的代码。Ajax 成功我收到 JSON 响应。响应采用数组格式。我想提醒响应。但是在"TypeError: invalid 'in' operand obj"JQuery 1.9.1 版本中出现错误。

回答by Amit Garg

success: function(response) {
        response=JSON.parse(response);
        $.each(response, function(key, val) {
        alert(val.id);
        });
    }

回答by Octavian

I got this error after I encoded twice a JSON array. Maybe it will help anybody.

我对 JSON 数组编码两次后收到此错误。也许它会帮助任何人。

回答by webmaster

I had the same problem. The error is being triggered from the jQuery function 'isArraylike( obj )' from the following line:

我有同样的问题。该错误是从以下行中的 jQuery 函数“isArraylike(obj)”触发的:

return type === "array" || type !== "function" &&
    ( length === 0 ||
    typeof length === "number" && length > 0 && ( length - 1 ) in obj );

The Javascript 'in' operator needs an object as operand, so chances are if you do jQuery.type(response) it'll show something other than an object (eg. string or null)

Javascript 'in' 运算符需要一个对象作为操作数,因此如果您执行 jQuery.type(response) ,它可能会显示对象以外的内容(例如字符串或空值)

So Amit's answer should work - if not, check the type of the response data and work from there.

所以 Amit 的答案应该有效 - 如果没有,请检查响应数据的类型并从那里开始工作。

回答by Codrutz Codrutz

Use dataType: "json"to have jQuery parse the response as JSON. It will solve your problem.

使用dataType: "json"拥有jQuery的解析响应为JSON。它会解决你的问题。

回答by Athafoud

Try this one, worked for me

试试这个,对我有用

$.each(eval(response), function(key, val)

For some reason (which I don't know), response is considered a string and not an object so you have to 'convert it' using eval().

出于某种原因(我不知道),响应被视为字符串而不是对象,因此您必须使用eval().

回答by Malcont3nt

I was getting this error while sending an array of column names to jQuery DataTables. The array needs to be an array of objects, not just an array of names. link to columns

我在向 jQuery DataTables 发送列名数组时收到此错误。该数组需要是一个对象数组,而不仅仅是名称数组。链接到列

BAD columns: ["Column1", "Column2"]

坏列:[“Column1”,“Column2”]

GOOD columns:[{title: "Column1"}, {title: "Column2"}]

好列:[{title: "Column1"}, {title: "Column2"}]

回答by fotanus

Same error, in my case instead of defining local as an array, I was defining it as a string. JSON.parseit did the trick.

同样的错误,在我的情况下,我没有将 local 定义为数组,而是将其定义为字符串。JSON.parse它做到了。