2012-04-01 18 views
1

我有一個JSON它看起來像這樣使用jQuery獲取JSON數據構建出一個表未定義

{ 
secInfo: [ 
{ 
    loginResult: 1, 
    userId: "admin", 
    userName: "admin", 
    userpassword: "ramshyam" 
}, 
{ 
    address: "", 
    loginResult: 1, 
    userEmail: "", 
    userId: "anuraag", 
    userName: "Anuraag", 
    userpassword: "kk0903" 
} 
] 
} 

當我使用下面的jQuery代碼顯示在表中它填充表「中的數據未定義」

<script> 
$(document).ready(function(){ 
    //attach a jQuery live event to the button 
    $('#submitlogin').live('click', function(){ 
     $.getJSON('/acc/services/json/security/loginresult.json', function(data) { 
      //alert(data); //uncomment this for debug 
      //alert (data.item1+" "+data.item2+" "+data.item3); //further debug 
      // $('#showdata').html("<p>Result="+data.loginResult+" userid="+data.userId+" username="+data.userName+"</p>"); 
      $.each(data.secInfo, function(){ 

       $('#usertable').append(
          function(this){ 
           return "<tr>"+ 
              "<td>"+this.userId+"</td>"+ 
              "<td>"+this.userName+"</td>"+ 
              "<td>"+this.userpassword+"</td>"+ 
              "<td>1</td>"+ 
             "<tr>"; 
          } 
          ); 

       }) 
     }); 
    }); 
}); 
</script> 

我是新來的jQuery並得到了一些想法來自http://forum.jquery.com/topic/jquery-create-table-from-json-data這個職位。

請幫我

回答

0

這絕對作品

<script> 
$(document).ready(function(){ 
    //attach a jQuery live event to the button 
    $('#submitlogin').live('click', function(){ 
     $.getJSON('/acc/services/json/security/loginresult.json', function(data) { 

    var genericlist = eval('(' + data+ ')'); 

var table = document.getElementById("usertable"); 
var tabledata = ""; 

for(i=0;i<genericlist.secInfo.length;i++){ 

tabledata += "<tr>"; 
tabledata += "<td>" + genericlist.secInfo[i].useID += "</td>"; 
tabledata += "<td>" + genericlist.secInfo[i].useName += "</td>"; 
tabledata += "<td>" + genericlist.secInfo[i].usePassword += "</td>"; 
tabledata += "</tr>"; 

} 

table.innerHTML = tabledata; 
0

的問題可能是「本」在你的回調函數,那是一個保留關鍵字。也可以嘗試以數組的形式訪問JSON條目而不是類屬性。

+0

如果仍然不能正常工作,嘗試的console.log(這個)你的回調中,或者將其更改爲另一個變量名,看什麼網站檢查員控制檯正在輸出。 – 2012-04-01 12:37:48

+0

好吧,由於JavaScript對象是由設計,所有關聯數組,它可能更容易像這樣['userId']來訪問它。另外,「this」指的是不同的範圍:) – 2012-04-01 12:43:29

+0

''這在控制檯上寫得很好,並使用userid進行提醒。不知道爲什麼它沒有得到函數內的對象 – antnewbee 2012-04-01 13:17:09

0

'我'的範圍已經改變了。試試這個:

$(document).ready(function(){ 
    //attach a jQuery live event to the button 
    $('#submitlogin').live('click', function(){ 
     $.getJSON('/acc/services/json/security/loginresult.json', function(data) { 
      //alert(data); //uncomment this for debug 
      //alert (data.item1+" "+data.item2+" "+data.item3); //further debug 
      // $('#showdata').html("<p>Result="+data.loginResult+" userid="+data.userId+" username="+data.userName+"</p>"); 
      $.each(data.secInfo, function(dataItem){ 
       $('#usertable').append(
        function() { 
         return "<tr>"+ 
            "<td>"+dataItem.userId+"</td>"+ 
            "<td>"+dataItem.userName+"</td>"+ 
            "<td>"+dataItem.userpassword+"</td>"+ 
            "<td>1</td>"+ 
           "<tr>"; 
        } 
       ); 
      }) 
     }); 
    }); 
}); 
+0

謝謝Mateusz ..但這也沒有工作:9 – antnewbee 2012-04-01 13:19:30