javascript - How do I solve 'Uncaught TypeError: Cannot use 'in' operator to search for 'length' in' and get my data to show up -
when make ajax request iterate on multiple arrays
uncaught typeerror: cannot use 'in' operator search 'length' in
$.get(url).done(function(data){ var data = json.stringify(data); var reviews = []; var output = '<div>'; $.each( data, function( k, v ) { $.each( data[k].mealreviews, function( key, value ) { output += '<div class="row">'; output += '<div class="col-sm-3 col-sm-offset-1 col-md-3 col-md-offset-1"><img class="img-thumbnail" src="images/' + value.accounttype +'.png" width="200" height="200" alt="">'; output += '<p>by <a>'+ value.username +'</a> '+ value.reviewdate +'</p></div>'; output += '<div class="col-sm-8 col-md-8"><div class="row">'; output += '<h2>'+ value.rating +'<span class="glyphicon glyphicon-star"></span> '+ value.subject +'</h2>'; output += '<p class="textformat">'+ value.review +'</p></div></div>'; output += '</div>'; }); }); output += '</div>'; reviews.push(output); $( '#mealdetails' ).append(output); });
var data = json.stringify(data);
intended turn existing json object string. once it's string, can call string functions on it. can't can normal object. you're getting error because turned data
string, , tried treat object. it's possible, since used jquery $.get
data
already string, because didn't specify json return datatype in ajax call, , therefore stringify
function had no effect.
you should able change code work:
$.getjson(url).done(function(data){ var reviews = []; var output = '<div>'; $.each( data, function( k, v ) { //...etc
the getjson
method downloads data json format automatically you. https://api.jquery.com/jquery.getjson/
Comments
Post a Comment