• Register
0 votes
546 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

1 vote
1 answer 7 views
7 views
Problem: Is there any way top avoid this error while using AJAX call ? Uncaught typeerror cannot read property length of undefined.
asked Apr 26 PkGuy 23.5k points
1 vote
1 answer 127 views
127 views
Problem : i'm using React router v^4.0.0, I've been having some trouble with react router. Router.js:43 Uncaught TypeError: Cannot read property 'location' of undefined at new Router (Router.js:43) at ReactCompositeComponent.js:295 at measureLifeCyclePerf ( ... .mountComponent (ReactReconciler.js:46) Any ideas on what I need to do to fix this? Thanks for your patience and help!!
asked Mar 21 Sabbir Hossain 14.6k points
0 votes
1 answer 1 view
1 view
Problem: var id = $('.getval_'+product_id+''+index+'').attr("id"); var value = ""; console.log(id); //data that return by id 6059 s2id_6061 6122 s2id_8410 if (id.indexOf('s2id_') > 0) { value = $('.getval_'+product_id+''+index+'').select2(' ... of ID don't have s2id_ it will go else statement. But the code return an error which is (Uncaught TypeError: Cannot read property 'indexOf' of undefined)
asked 21 hours ago ochena 22.1k points
0 votes
1 answer 3 views
3 views
Problem: I am learning javascript from Mozilla developer network(MDN). I am getting error in Number guessing game. The error says "Uncaught TypeError: Cannot read property 'addEventListener' of null" at line 46.
asked 4 days ago padma1 34.3k points
1 vote
1 answer 4 views
4 views
Problem: I need the solution of this error: Uncaught typeerror: cannot read property 'top' of undefined.
asked Apr 27 PkGuy 23.5k points
1 vote
1 answer 17 views
17 views
Problem : I'm trying to add jquery validation to containing ,I have a preexisting form.I use jquery validator plugin. I'm getting the error : Uncaught TypeError: Cannot read property 'call' of undefined I have no idea How to fix this.
asked Mar 21 Sabbir Hossain 14.6k points
1 vote
1 answer 26 views
26 views
Problem : First time I pushing an array's contents to another array by seeing tutorial and I get this: "Uncaught TypeError: Cannot read property 'push' of undefined" Here is my code : var order = new Object(), stack = []; for(var i=0;i<a.length;i++){ if(parseInt(a[i].daysleft) == 0 ... ); } if(parseInt(a[i].daysleft) < 0){ order[2].push(a[i]); } }Any ideas on what I need to do to fix this? Thanks!
asked Mar 21 Sabbir Hossain 14.6k points
1 vote
1 answer 32 views
32 views
Problem : I am trying to execute the function manually.When i try to load a select list on document ready i get this error : Uncaught TypeError: Cannot read property 'toLowerCase' of undefined -> jquery-2.1.1.js:7300 Here is the code : $(document).ready(function() { $(" ... teacher.name); $('#TeacherSelect').append(option); }); }, error: function() { alert("Error while getting results"); } }); }
asked Mar 20 Sabbir Hossain 14.6k points
1 vote
1 answer 31 views
31 views
Problem : Why I got the error : *Uncaught TypeError: Cannot read property 'name' of undefined * I am new in javascript. Can you tell me what the problem is here?
asked Mar 20 Sabbir Hossain 14.6k points