• Register
0 votes

Problem :

I am new to JSON. I am facing below error if I try to do a $.each to my following JSON object.

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

JSON object as below :
{"type":"Anuncio","textos":["Probando esto","$ 20150515"],"submit":"codParameters?___DDSESSIONID\u003d14EA4721A904D6DD71591156996E29F7%3A%2FMobilTest"}
How to fix above JSON error ?
8 4 2
2,300 points

1 Answer

0 votes

Solution :

I think you have forgot to add the parameter dataType:'json' in your $.ajax as shown below
   type: "POST",
   dataType: "json",
   url: url,
   data: { get_member: id },
   success: function( response ) 
     //your some action here
   error: function( error )
     alert( error );

Also you must parse your string in your anyValue variable to the object:

prePopulate: $.parseJSON(anyValue)

You can try this in plain JS as shwn below:

prePopulate: JSON.parse(anyValue)
You might face above error if you are using the string as an array. Suppose if you got a json from the ajax, and you forgot to parse your result and after that using that result as the array. So the solution to it is to parse your json before using it.
Your in operator will only work on objects. And it seems you are trying to use it on a string. So you need to make sure that your value is an object before you try to use $.each. In your case, you can also parse the JSON:
$.each(JSON.parse(anyValue), ...);


5 2 1
4,980 points

Related questions

0 votes
1 answer 65 views
Problem: I am unable to fix the below listed Datatables error: "SCRIPT5007: Unable to set property '_DT_CellIndex' of undefined or null reference." I have already tried to look for the solution all over the internet and but unable to find the best solution for my ... am currently unable to fix this issue. Is there anything which I am missing here? Please Note: I am very newbie with JavaScript.
asked Jul 16 Raphael Pacheco 4.9k points
0 votes
1 answer 142 views
Problem: I have fundamental knowledge in PHP. Recently I have written a php code for bootstrap collapse but I am facing the error as below: "Cannot read property 'querySelectorAll' of null." I have spent a huge amount of time to this very simple problem. If I try ... js:378) at Function.each (jquery.js:368) at jQuery.fn.init.each (jquery.js:157) at HTMLButtonElement.<anonymous> (collapse.js:374)
asked Jul 4 Raphael Pacheco 4.9k points