如何通过bootstrapTable()填充bootstraptable?

How to fill bootstrap table via bootstrapTable()?

我正在尝试用 json 数据填充 bootstrap table。 这是整个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"}]

但是 table 是空的,我卡在了 "Loading, please wait" 消息上。 我做了一个 fiddle 只是为了测试,一切都很好。那么,我的代码有什么问题?

您的回复采用 text/html 格式。 你应该将它解析为 JSON object

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

默认(a.k.a。自动检测)$.get 返回像 text/html 编码 https://api.jquery.com/jquery.get/

您可以向 ajax 调用添加其他选项 "dataType:json" 以指定返回的对象是 JSON。