JQuery 用Ajax 获取Json数据并显示

JQuery 用Ajax 获取Json数据并显示




JQuery:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
 <script>
        $(function(){
            $(':input:eq(0)').click(function(){
                $.ajax({
                    url: 'data.json',
                    type: 'get',
                    dataType: 'json',
                    success: function(datas){
                        var tbhtml = "姓名年龄性别";
                        for(var i = 0;i<datas.length;i++){
                                tbhtml+=""+datas[i].name+""+datas[i].age+""+datas[i].sex+"";
                        }

                        $('table:eq(0)').html(tbhtml);
                    }
                })
            })
        })
        </script>


HTML:

		<center>
       
            <table border="1px solid">
                <tr>
                    <th>姓名th>
                    <th>年龄th>
                    <th>性别th>
                  tr>
            table>
            <input type="button" value="加载数据" />
        center>

Json:

 [
    {
        "name": "zhangsan",
        "age": 18,
        "sex": "man"
    },
    {
        "name": "lisi",
        "age": 17,
        "sex": "man"
    },
    {
        "name": "wangwu",
        "age": 16,
        "sex": "man"
    }
]

你可能感兴趣的:(JQuery,JS)