ajax读取txt文本中的json并显示在html页面表格上

txt文本

[{"name":"minmin","age":"21"},{"name":"huahua","age":"22"},{"name":"jiji","age":"23"}]

js

<script src="jquery.js" charset="utf-8"></script>
    <script>
        window.onload=function(){
            $.ajax({
                type: "GET",
                url: "data/json.txt",
                dataType: "json",
                success: function(result){
                    let json=result;
                    console.log(json);
                    for(i=0;i<json.length;i++){
                        var trTD = "<tr><td>"+json[i].name+"</td><td>"+json[i].age+"</td></tr>";
                        $("#tb").append(trTD);
                    }
                },
                error: function(){
                    alert("失败了");
                }
            });
        }();
    </script>

html

     <div>
        <table  style="border: 1" id="tb"><tr><td>姓名</td><td>年龄</td></tr></table>  
     </div>

以上内容借鉴https://blog.csdn.net/qq_20916285/article/details/46695839

posted @ 2020-03-22 00:16  海常7号  阅读(824)  评论(0编辑  收藏  举报