Javascript 未捕获的类型错误:无法使用“in”运算符来搜索“length”

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

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in

javascriptjqueryjsongetjson

提问by Iván Alberto Fontalvo Salgado

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in "

未捕获的类型错误:无法使用“in”运算符在“中”搜索“长度”

This is the error I receive when I try to do a $.eachto this JSON object :

这是我尝试$.each对此 JSON 对象执行操作时收到的错误:

{"type":"Anuncio","textos":["Probando esto","$ 20150515"],"submit":"codParameters?___DDSESSIONID\u003d14EA4721A904D6DD71591156996E29F7%3A%2FMobilTest"}

I have also tried to do the same with stringify, but I receive the same error:

我也试过用 stringify 做同样的事情,但我收到了同样的错误:

{\"type\":\"Anuncio\",\"textos\":[\"Probando esto\",\"$ 20150515\"],\"submit\":\"codParameters?___DDSESSIONID\u003d06CBEC9D1A53616EFF703A8C71FBC2B4%3A%2FMobilTest\"}"

If I remove parameters ___DDSESSIONID\\u003d06CBEC9D1A53616EFF703A8C71FBC2B4%3A%2FMobilTestfrom the object the $.each works fine.

如果我___DDSESSIONID\\u003d06CBEC9D1A53616EFF703A8C71FBC2B4%3A%2FMobilTest从对象中删除参数$.each 工作正常。

Why might this be happening?

为什么会发生这种情况?

Thanks in advance.

提前致谢。

回答by Felix Kling

The inoperator only works on objects. You are using it on a string. Make sure your value is an object before you using $.each. In this specific case, you have to parse the JSON:

in运算符仅适用于对象。您正在字符串上使用它。在使用之前,请确保您的值是一个对象$.each。在这种特定情况下,您必须解析 JSON

$.each(JSON.parse(myData), ...);

回答by Tri W. Herlambang

maybe you forget to add parameter dataType:'json' in your $.ajax

也许您忘记在 $.ajax 中添加参数 dataType:'json'

$.ajax({
   type: "POST",
   dataType: "json",
   url: url,
   data: { get_member: id },
   success: function( response ) 
   { 
     //some action here
   },
   error: function( error )
   {
     alert( error );
   }
});