weixin_33682790 2017-09-14 10:19 采纳率: 0%
浏览 34

JSON至Ajax中的HTML表

This is the JSON i have

{
   "version": "5.2",
   "user_type": "online",
   "user":
   [
       {
            "name": "John",
            "id": 50
       },
       {
            "name": "Mark",
            "id": 57
        }
    ]
}

The javascript to convert the above JSON to HTML

<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        url: "http://PATH/user.json",
        dataType: 'json',
        type: 'get',
        cache:false,
        success: function(data){
            /*console.log(data);*/
            var event_data = '';
            $.each(data, function(index, value){
                /*console.log(value);*/
                event_data += '<tr>';
                event_data += '<td>'+value.name+'</td>';
                event_data += '<td>'+value.id+'</td>';
                event_data += '<tr>';
            });
            $("#list_table_json").append(event_data);
        },
        error: function(d){
            /*console.log("error");*/
            alert("404. Please wait until the File is Loaded.");
        }
    });
});
</script>

The HTML code for table :

<table class="table table-responsive table-hover table-bordered" id="list_table_json">
    <thead>
        <tr>
            <th>Name</th>
            <th>ID</th>                  
        </tr>                   
    </thead>
</table>

Did not get any error's in the console

The output i get in the table says undefined. how to push the data to json ?

  • 写回答

3条回答 默认 最新

  • weixin_33743703 2017-09-14 10:26
    关注

    Your loop should be like $.each(data.user, function(index, value){}); and close </tr> in end

    <script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url: "http://PATH/user.json",
            dataType: 'json',
            type: 'get',
            cache:false,
            success: function(data){
                /*console.log(data);*/
                var event_data = '';
                $.each(data.user, function(index, value){
                    /*console.log(value);*/
                    event_data += '<tr>';
                    event_data += '<td>'+value.name+'</td>';
                    event_data += '<td>'+value.id+'</td>';
                    event_data += '</tr>';
                });
                $("#list_table_json").append(event_data);
            },
            error: function(d){
                /*console.log("error");*/
                alert("404. Please wait until the File is Loaded.");
            }
        });
    });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目