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

334,985

Solution 1

The in operator 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:

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

Solution 2

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

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

Solution 3

this work for me

$.each(JSON.parse("[" + data + "]"))

$.each only works on objects . if your data is an array you can change it like this

 $data = (object)$array;
Share:
334,985

Related videos on Youtube

Iván Alberto Fontalvo Salgado
Author by

Iván Alberto Fontalvo Salgado

Updated on October 05, 2021

Comments

  • Iván Alberto Fontalvo Salgado
    Iván Alberto Fontalvo Salgado over 2 years

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

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

    {"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:

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

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

    Why might this be happening?

    • Sidd
      Sidd almost 9 years
      Please format your code, it's impossible to read it like this. You can use the {} text editor button, or indent every code line 4 spaces.
  • Iván Alberto Fontalvo Salgado
    Iván Alberto Fontalvo Salgado almost 9 years
    Felix, before $.each I have JSON.parse , the error appears when in the key submit have the value -> "codParameters?___DDSESSIONID\u003d14EA4721A904D6DD715911569‌​96E29F7%3A%2FMobilTe‌​st".
  • Felix Kling
    Felix Kling almost 9 years
    Please post your code, I can't repro the error you are describing: jsfiddle.net/5asbeatq
  • Felix Kling
    Felix Kling almost 9 years
    Don't post an answer if it isn't one. Edit your question and include the relevant code in the question.
  • Mohammed Sufian
    Mohammed Sufian over 7 years
    wasted 2 hours, and finally got it working using validator.showErrors(JSON.parse(data)) , thanks a lot sir..
  • Cr1xus
    Cr1xus almost 6 years
    I get this error for $.each("ul li",function(k,v){ ... });
  • Luis Rock
    Luis Rock over 4 years
    That did it to me. Thanks!
  • Raju Paladiya
    Raju Paladiya over 4 years
    Thanks @FelixKling, You saved my lot's of time. After convert string to Array it's working fine.
  • Mech
    Mech about 4 years
    Thanks from 2020!
  • Jesper
    Jesper over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review