如何通过bootstrapTable()填充bootstrap表?

时间:2019-06-17 08:13:42

标签: javascript jquery bootstrap-table

我正在尝试用json数据填充引导表。 这是整个index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="https://unpkg.com/bootstrap-table@1.14.2/dist/bootstrap-table.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script>
        $.get("http://localhost:8081/cash/list", function (data) {
            console.log(data);
            $(function () {
                $('#table').bootstrapTable({
                    data: data
                });
            });
        });
    </script>
</head>
<body>
<div class="container">
    <table id="table" class="table">
        <thead>
        <tr>
            <th data-field="IpAddr">ip</th>
            <th data-field="FactoryNumber">factory</th>
            <th data-field="Unsent">unsent</th>
            <th data-field="Os">os</th>
            <th data-field="ExpirationDate">expiration</th>
            <th data-field="Version">version</th>
        </tr>
        </thead>
    </table>
</div>
</body>
</html>

我在控制台中收到以下响应:

[{"IpAddr":"10.99.220.7","FactoryNumber":"34567","Unsent":10,"Os":"windows","ExpirationDate":"05-05-2021","Version":"1.1067"},{"IpAddr":"10.99.228.228","FactoryNumber":"142123951023","Unsent":1,"Os":"linux","ExpirationDate":"05-05-2020","Version":"1.1067"},{"IpAddr":"10.99.220.7","FactoryNumber":"1234567","Unsent":2,"Os":"windows","ExpirationDate":"05-05-2021","Version":"1.1067"},{"IpAddr":"10.99.220.7","FactoryNumber":"234567","Unsent":3,"Os":"windows","ExpirationDate":"05-05-2021","Version":"1.1067"}]

但是表为空,我停留在“正在加载,请稍候”消息。 我做了一个fiddle只是为了测试,那里一切都很好。那么,我的代码怎么了?

1 个答案:

答案 0 :(得分:0)

您的回复为text / html格式。 您应该将其解析为JSON对象

$('#table').bootstrapTable({
  data: JSON.parse(data)
});

默认情况下(也称为自动检测),$。get的返回方式类似于文本/ html编码 https://api.jquery.com/jquery.get/

您可以在ajax调用中添加其他选项“ dataType:json”,以指定返回的对象为JSON。