我创建了一个ajax json响应,该响应将显示在我的dataTable jQuery插件中。该表的ID为#dataTable。
这是dataTable插件的代码:
$(document).ready(function() {
var productTable = $("#dataTable").DataTable({
"ajax": "../api/ajax/getProduct.php","order": [[ 1,"desc" ]]
});
});
这是getProduct.php
<?php
include_once('../../components/db.php');
$sqlb = "SELECT * FROM products WHERE status='active'";
$resultb = $conn->query($sqlb);
$data = $resultb->fetch_assoc();
$result = array();
foreach ($data as $key => $value) {
$image = '<img width="50" height="40" class="rounded-circle" src="../' . $value['image'] . '; ?>">';
$buttons = '<a href="product-update.php' . $value["id"] . '" class="btn btn-info btn-sm"><i class="fa fa-edit" aria-hidden="true"></i></a><a onclick="removeProduct(' . $value["id"] . ')" class="btn btn-danger btn-sm"><i class="fa fa-trash" aria-hidden="true"></i></a>';
$result[$key] = array(
$value["description"],$value["price"],$image,$value["availability"],$buttons,);
}// /foreach
echo json_encode($result);
?>
这是我检查XHR时遇到的错误
Warning: Illegal string offset 'image' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 11
Warning: Illegal string offset 'id' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 13
Warning: Illegal string offset 'id' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 13
Warning: Illegal string offset 'description' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 16
Warning: Illegal string offset 'price' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 17
Warning: Illegal string offset 'availability' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 19
这是我在页面加载时从dataTable本身得到的弹出错误。
DataTables warning: table id=dataTable - Invalid JSON response. For more information about this error,please see http://datatables.net/tn/1
似乎真正的问题是编码的数据本身是错误的。遇到此错误,我无法将数据显示到dataTable本身中。