jquery - 如何使用 jquery datatable 显示来自 url 的 API 数据

标签 jquery ajax api url datatables

我正在为 FiveM 的服务器网站设置一个“统计”页面,我需要有关如何使用 Jquery Datatables 显示数据的帮助

我对API一无所知,所以我用PHP尝试了一些东西,但没有什么真正有用的:/

这是我的代码:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>test-astos</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
  </head>
  <body>

    <table id="table_id" class="display">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
        </thead>
        <tbody>
            <!-- data -->
        </tbody>
    </table>


    <script type="text/javascript">
    $(document).ready( function () {
      $('#table_id').DataTable({
        $.ajax({
          url : 'https://api.top-serveurs.net/v1/servers/SC4VCSEUS3/players-ranking',
          type : 'GET',
          dataType : 'json',
             success : function(json, statut){ // code_html contient le HTML renvoyé
             }
        });
      });
    });
    </script>

  </body>
</html>

我想使用 Jquery 数据表显示这些数据 ( https://api.top-serveurs.net/v1/servers/SC4VCSEUS3/players-ranking )。

谢谢:)。

最佳答案

首先,您忘记使用 jQuery CDN(参见示例)。并像这样使用。

<table id="table_id" class="display">
    <thead>
        <tr>
            <th>Votes</th>
            <th>Player Name</th>
        </tr>
    </thead>
    <tbody>
        <!-- data -->
    </tbody>
</table>


<script type="text/javascript">
$(document).ready( function () {

    $.ajax({
        url : 'https://api.top-serveurs.net/v1/servers/SC4VCSEUS3/players-ranking',
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            bindtoDatatable(data.players);
        }
    });



});

function bindtoDatatable(data) {
        var table = $('#table_id').dataTable({
            "bAutoWidth" : false,
            "aaData" : data,
            "columns" : [ {
                "data" : "votes"
            }, {
                "data" : "playername"
            } ]
        })
    }
</script>

关于jquery - 如何使用 jquery datatable 显示来自 url 的 API 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56417401/

相关文章:

algorithm - map API : Finding the longest common path in two given paths

javascript - Jquery如何改变选择<option>框中的一个单词的颜色

javascript - 响应新 DOM 的函数

jquery - 棘手 : Surpress "Loading ..." title in Firefox for JSONP polling

php - First Data PHP API

python - 嵌套序列化器中的 Django Rest Framework 自定义 URL

javascript - jQuery 函数的问题

javascript - 为什么 jQuery 代码片段在没有 IFrame 的情况下可以工作,但在有 IFrame 的情况下却不能?

jquery - 如何从 jquery 移动单选按钮中删除边框

javascript - 标题中的提交按钮无法提交