未捕获的TypeError:不能使用'in'运算符来搜索'length'

问题描述 投票:74回答:2

未捕获的TypeError:无法使用'in'运算符搜索“length”中的“length”

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

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

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

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

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

为什么会发生这种情况?

提前致谢。

javascript jquery json getjson
2个回答
187
投票

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

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

5
投票

也许你忘了在$ .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 );
   }
});
© www.soinside.com 2019 - 2024. All rights reserved.