0

I'm trying to display items retrieved from the database in webpage using Jquery. But my code is failed to do that. Anyone tell me how to display the items from array.

My code is :

  success: function( data, textStatus, jqXHR) 
        {
            if(data.success)
            {
                for(var i = 0,len=data.length;i<len;i += 1){
                    if(data.commentInfo[i].success)
                    {
                        var newcommhtml = '<div id="c0'+thecid+'" class="cnew clearfix"> <section class="c-author">';
                        newcommhtml = newcommhtml + '<h3>Anonymous</h3>';
                        newcommhtml = newcommhtml + '<span class="pubdate">'+month+' '+day+', '+year+'</span> </section>';
                        newcommhtml = newcommhtml + '<section class="c-content">';
                        newcommhtml = newcommhtml + '<img src="images/green-avatar.png" alt="avatar" width="80" height="80" class="ava">';
                        newcommhtml = newcommhtml + '<p>'+nl2br(data.commentInfo[i].comment)+'</p> </section></div>';

                        var thelm = "#c0"+thecid;
                        commwrap.append(newcommhtml);
                        $(thelm).hide().fadeIn('slow');

                        setTimeout(function() { $(thelm).addClass('green'); }, 800);

                        $("#comm").val("");
                        thecid++;

                    }
                    else
                        {
                        alert("dsdsds");
                        }
                }
                    if(errorspan.html() != null) {
                        errorspan.remove();
                    }
            }

          },
     error: function(jqXHR, textStatus, errorThrown)
      {
         alert("error"+errorThrown);
         console.log("Something really bad happened " + textStatus);
      },

And the response received from the server is :

 {"success":true,"commentInfo":[{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"},{"uname":"shyam","comment":"ghg"}]} 

Please anyone help me .... Thanks....

James
  • 2,874
  • 4
  • 30
  • 55

1 Answers1

3

data is an object not an array.

If you wish to target commentInfo in data which is an array you would do:

for (var i = 0; i < data.commentInfo.length; i++) {
   var item = data.commentInfo[i];
}
Mechlar
  • 4,946
  • 12
  • 58
  • 83