【发布时间】:2017-08-01 16:15:07
【问题描述】:
我已经下载并安装了正确的部件来使用制表器我有一个例子来展示。 现在我正在尝试从我的 localhost 数据库中提取数据以用于简单的 Ajax 网格。
这是我在名为 query.php 的文件中使用的 PHP 代码
<?php
$db = new mysqli('localhost', 'root', 'PASS');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$result = $db->query($db, "SELECT * FROM dataBase.table;");
if ($result) {
$to_encode = array();
while ($row = mysqli_fetch_assoc($result)) {
$to_encode[] = $row;
}
echo json_encode($to_encode);
}
这是我的 index.html 的 HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Bitnami: Open Source. Simplified</title>
<link href="bitnami.css" media="all" rel="Stylesheet" type="text/css"/>
</head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script type="text/javascript" src="tabulator/dist/js/tabulator.min.js">
</script>
<div id="example-table">
<script type="text/javascript">
$.getJSON('query.php', function (data) {
var mydata = $.parseJSON(data);
$("#example-table").tabulator("setData", mydata);
});
</script>
</div>
</body>
</html>
由于某种原因,我得到一个空白页,如果您有任何建议,请告诉我。
【问题讨论】:
-
初学者需要检查
$.getJSON回调是否被触发。如果它确实复制了 json 并查看插件是否可以在没有 ajax 的情况下传入数据。如果未触发回调,请添加一些错误处理。没有一些基本的故障排除信息,就会有太多未知数 -
同时检查浏览器控制台是否有错误
标签: php jquery mysql ajax tabulator