0

我正在尝试使用 Jquery 在网页中显示从数据库中检索到的项目。但我的代码未能做到这一点。任何人都告诉我如何显示数组中的项目。

我的代码是:

  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);
      },

从服务器收到的响应是:

 {"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"}]} 

请任何人帮助我....谢谢....

4

1 回答 1

3

data是一个对象而不是一个数组。

如果您希望以commentInfo数组的数据为目标,您可以:

for (var i = 0; i < data.commentInfo.length; i++) {
   var item = data.commentInfo[i];
}
于 2013-02-21T17:28:28.653 回答