我想使用REST API和jQuery插件DataTable从https://swapi.co/api/planets/?format=json数据中获取所有json,但是我的问题是,它首先加载了数据,但是当我开始输入Datatable提供的搜索字段时。 ,它显示“ 表中没有数据”。
我一直在搜索类似的问题,但仍然找不到解决方案。我尝试过的是
我的HTML文件:
<table id="tableSwapi" class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Rotation Period</th>
<th>Orbital Period</th>
<th>Diameter</th>
<th>Climate</th>
<th>Gravity</th>
<th>Terrain</th>
<th>Water Surface</th>
<th>Population</th>
</tr>
</thead>
<tbody id="list-list">
</tbody>
</table>
我的脚本文件:
$(document).ready(function () {
$("#tableSwapi").dataTable();
$.ajax({
url: 'https://swapi.co/api/planets/',
type: 'get',
dataType: 'json',
success: function (result) {
let daftar = result.results;
var html = '';
$.each(daftar, function (i, data) {
html += `<tr>
<td> ` + data.name + `</td>
<td>` + data.rotation_period + `</td>
<td>` + data.orbital_period + `</td>
<td>` + data.diameter + `</td>
<td> ` + data.climate + ` </td>
<td> ` + data.gravity + ` </td>
<td>` + data.terrain + `</td>
<td> ` + data.surface_water + `</td>
<td> ` + data.population + ` <br></td>
</tr>`;
//This is selector of my <tbody> in my table
$("#list-list").html(html);
});
}
});
})
任何帮助都将受到赞赏。谢谢。
答案 0 :(得分:2)
如果您可以使用服务器端脚本,请尝试使用类似的代码
PHP代码ajax.php
$url = "https://swapi.co/api/planets/?page=".($_GET['start']/$_GET['length']+1);
if (isset($_GET['search']) && !empty($_GET['search'])) {
$url .= "&search=".$_GET['search']['value'];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = json_decode(curl_exec($ch),true);
$array = array
(
"draw" => $_GET['draw'],
"recordsTotal" => $result['count'],
"recordsFiltered" => $result['count'],
"data" => $result['results'],
);
echo json_encode($array);
jQuery数据表代码
$('#tableSwapi').DataTable({
"processing": true,
"serverSide": true,
"sPaginationType": "full_numbers",
"order": [],
"ajax": {
"url": "ajax.php",
"type": 'get',
"dataType": 'json'
},
"columns": [
{ "data": "name" },
{ "data": "rotation_period" },
{ "data": "orbital_period" },
{ "data": "diameter" },
{ "data": "climate" },
{ "data": "gravity" },
{ "data": "terrain" },
{ "data": "surface_water" },
{ "data": "population" },
]
});
答案 1 :(得分:1)
我使用了您的示例,它可以正常工作。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
</head>
<body>
<table id="tableSwapi" class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Rotation Period</th>
<th>Orbital Period</th>
<th>Diameter</th>
<th>Climate</th>
<th>Gravity</th>
<th>Terrain</th>
<th>Water Surface</th>
<th>Population</th>
</tr>
</thead>
<tbody id="list-list"></tbody>
</table>
<script
src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$("#tableSwapi").dataTable();
$.ajax({
url: 'https://swapi.co/api/planets/',
type: 'get',
dataType: 'json',
success: function (result) {
let daftar = result.results;
var html = '';
$.each(daftar, function (i, data) {
html += `<tr>
<td> ` + data.name + `</td>
<td>` + data.rotation_period + `</td>
<td>` + data.orbital_period + `</td>
<td>` + data.diameter + `</td>
<td> ` + data.climate + ` </td>
<td> ` + data.gravity + ` </td>
<td>` + data.terrain + `</td>
<td> ` + data.surface_water + `</td>
<td> ` + data.population + ` <br></td>
</tr>`;
//This is selector of my <tbody> in my table
$("#list-list").html(html);
});
}
});
})
</script>
</body>
</html>
Datatable插件可能有问题。请检入检查元素。