服务器响应的数据是以数组形式的例如:
arr=[
{id:1,name:'iPhone4',price:1999},
{id:2,name:'iPhone5',price:2999},
{id:3,name:'iPhone6',price:3999},
{id:4,name:'iPhone7',price:4999},
{id:5,name:'iPhone7s',price:5999}
];
如果想要把数据响应到浏览器页面表格中,则需要使用循环遍历数组,然后添加到表格中
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 综合展示表格的动态数据 -->
<table border="2px" cellpadding="1px" cellspacing="0" width="400px">
<thead>
<tr>
<th>编号</th>
<th>商品名</th>
<th>商品价格</th>
</tr>
</thead>
<tbody id="tab">
</tbody>
</table>
<script type="text/javascript">
//提交服务器数据
var arr = [
{id:1,name:'iPhone4',price:1999},
{id:2,name:'iPhone5',price:2999},
{id:3,name:'iPhone6',price:3999},
{id:4,name:'iPhone7',price:4999},
{id:5,name:'iPhone7s',price:5999}
];
//1.创建字符串
var str=''
//2.创建循环遍历每个数据
for(let i=0;i<arr.length;i++){
//2.1创建变量,拼接字符串
str+=`<tr><td>${arr[i].id}</td><td>${arr[i].name}</td><td>${arr[i].price}</td></tr>`
}
tab.innerHTML=str
</script>
</body>
</html>