<!DOCTYPE html>
<html>
<head>
<title>千锋教育——做真实的自己,用心做教育</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<button id="btn">点我加载一组</button>
<button id="btn">点我加载第二组</button>
<table id="bookinfo" border="1px">
<!-- <tr>
<td></td>
<td></td>
<td></td>
</tr> -->
</table>
<script>
var oBookInfo = document.querySelector('#bookinfo');
var oBtn = document.querySelector('#btn');
var str = '';
oBtn.onclick = function () {
getTables();
}
function getTables(n) {
var xhr = new XMLHttpRequest();
xhr.open('get', '08_json.php');
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
var arr = JSON.parse(xhr.responseText).data;
arr.forEach(function (item, index) {
str += ` <tr>
<td>${item.name}</td>
<td>${item.category}</td>
<td>${item.desc}</td>
</tr>`
})
oBookInfo.innerHTML = str;
}
}
}
</script>
</body>
</html>
<?php
$jsonstr = '{
"total":"4",
"data":[
{
"name":"三国演义",
"category":"文学",
"desc":"一个军阀混战的年代"
},{
"name":"水浒传",
"category":"文学",
"desc":"草寇or英雄好汉"
},{
"name":"西游记",
"category":"文学",
"desc":"妖魔鬼怪牛鬼蛇神什么都有"
},{
"name":"红楼梦",
"category":"文学",
"desc":"一个封建王朝的缩影"
}
]
}';
echo $jsonstr;
?>