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

JavascriptJqueryJsonGetjson

Javascript Problem Overview


> 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?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

The only solution that worked for me and $.each was definitely causing the error. so i used for loop and it's not throwing error anymore.

Example code

     $.ajax({
            type: 'GET',
            url: 'https://example.com/api',
            data: { get_param: 'value' },
            success: function (data) {
                for (var i = 0; i < data.length; ++i) {
                    console.log(data[i].NameGerman);
                }
            }
        });

Solution 4 - Javascript

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIv&#225;n Alberto Fontalvo SalgadoView Question on Stackoverflow
Solution 1 - JavascriptFelix KlingView Answer on Stackoverflow
Solution 2 - JavascriptTri W. HerlambangView Answer on Stackoverflow
Solution 3 - JavascriptAmir Dora.View Answer on Stackoverflow
Solution 4 - JavascriptSaeidhpView Answer on Stackoverflow