• Register
0 votes
242 views

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

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I think you have forgot to add the parameter dataType:'json' in your $.ajax as shown below
$.ajax({
   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 12 views
0 votes
1 answer 193 views
193 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 358 views
358 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, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 388 views
388 views
Problem: I have elementary knowledge of Jquery and Javascript. Currently I am working on the pagination and I am trying to use the DataTables plugin. I have observed that on some of the tables it works as expected but on some of my tables it gives below error: " ... it is very common error but I am unable to find anything related to my problem. Kindly help me if any Jquery expert is around.
asked Jun 2, 2020 Martin K 6.6k points
0 votes
1 answer 237 views
237 views
Problem : Facing following chrome error error in event handler for (unknown): typeerror: cannot read property 'indexof' of undefined
asked Nov 16, 2019 peterlaw 6.9k points
0 votes
1 answer 16 views
16 views
Problem: I want to create the search box for the states, districts and villages but because of some unknown reason to me I am just unable to fetch my data into the combobox. Please find my code as shown below: My HTML code: <div class="dummy__item">  < ... below task: Dynamically populate a select element from json data with jquery . I am looking for the expert in Javascript , Jquery and Json.
asked Jul 2, 2020 Raphael Pacheco 4.9k points