Search code examples
javascriptjquerybootstrap-table

How to fill bootstrap table via bootstrapTable()?


I'm trying to fill bootstrap table with json data. Here is the whole 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>

I'm getting the following response in console :

[{"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"}]

But the table is empty and I'm stuck on "Loading, please wait" message. I've made a fiddle just for a test and everything is fine there. So, what's wrong with my code?


Solution

  • Your response is in text/html format. You should parse it to JSON object

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

    By default (a.k.a. auto detecting) $.get is returning like text/html encoding https://api.jquery.com/jquery.get/

    You can add additional options "dataType:json" to you ajax call to specify that the returned object is JSON.