我有一個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這個職位。
請幫我
如果仍然不能正常工作,嘗試的console.log(這個)你的回調中,或者將其更改爲另一個變量名,看什麼網站檢查員控制檯正在輸出。 – 2012-04-01 12:37:48
好吧,由於JavaScript對象是由設計,所有關聯數組,它可能更容易像這樣['userId']來訪問它。另外,「this」指的是不同的範圍:) – 2012-04-01 12:43:29
''這在控制檯上寫得很好,並使用userid進行提醒。不知道爲什麼它沒有得到函數內的對象 – antnewbee 2012-04-01 13:17:09