嗨,我是編程新手。我想使用jQuery在HTML表格中顯示Json數據。以html表格格式顯示json數據
輸出來自服務器:
{"status":1,"insert":1,"data":[{"id":"1","name":"Jatin","email":"[email protected]","status":"1"},{"id":"2","name":"Shubham","email":"[email protected]","status":"1"},{"id":"3","name":"Aneh","email":"[email protected]","status":"1"},{"id":"52","name":"php","email":"[email protected]","status":"test"},{"id":"53","name":"faf","email":"as","status":"d"},{"id":"54","name":"taus","email":"alksd","status":"fdasd"},{"id":"55","name":"nake","email":"nakk","status":"naii"},{"id":"56","name":"test1","email":"tausf","status":"dfasd"},{"id":"57","name":"","email":"","status":""},{"id":"58","name":"shubu","email":"[email protected]","status":"12"},{"id":"59","name":"panku","email":"[email protected]","status":"3"},{"id":"60","name":"Jatin","email":"[email protected]","status":"123d"}]}
HTML代碼:
<table border="1" align="center">
<tr>
<td> <input type="button" id="display" value="Display All Data" /> </td>
</tr>
</table>
<div id="responsecontainer" align="center">
</div>
的jQuery:
<script>
$(document).ready(function() {
$("#display").click(function() {
$.ajax({ //create an ajax request to load_page.php
url:'http://localhost/webservice/php_webservices/WebServices.php?method=select',
type: "POST",
dataType: "html", //expect html to be returned
success: function(response){
$("#responsecontainer").html(response);
//alert(response);
}
});
});
});
</script>
嘗試一些處理響應,轉換爲javascript對象,執行數組中每個項目的循環,通過字符串操作創建所需的html tr-td,並且您很好。 – Dharmang
或結帳AngularJS(表格示例http://www.w3schools.com/angular/angular_tables.asp)。它可以讓你使用幾行代碼。 – Fredster