2013-02-21 76 views
0

我試圖顯示使用Jquery從網頁中的數據庫檢索項目。但是我的代碼沒有做到這一點。任何人都會告訴我如何顯示數組中的項目。如何使用Jquery顯示JSONArray項目

我的代碼是:

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

請人幫我....謝謝....

+0

'data'是一個對象,而不是一個陣列,它沒有一個'length'屬性。 – 2013-02-21 17:28:32

回答

3

data是一個對象不是數組。

如果你想在這是一個數組,你會做數據到目標commentInfo

for (var i = 0; i < data.commentInfo.length; i++) { 
    var item = data.commentInfo[i]; 
} 
+1

可能他也應該測試'if(data.success && data.commentInfo.length){' – Blazemonger 2013-02-21 17:30:40