加载代码2

<!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 = '';


        //  如果想获取不同数据,可以利用请求参数,传入的1,返回的json字符串1,传入的是2,的话,返回json字符串2
        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);
                    //  json形式的字符串如何转为 数组
                    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;

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值