数据表 ajax 分页有问题,它给出未定义的 url
Problem with datatable ajax pagination, it give undefined url
它给出:
http://localhost/aps/undefined Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.35 (Win32) OpenSSL/1.1.0i PHP/7.2.11
//The php code to fetch data
<?php
include('db.php');
$query = '';
$output = array();
$query .= "SELECT * FROM users ";
if(isset($_POST["search"]["value"]))
{
$query .= 'WHERE first_name LIKE "%'.$_POST["search"]["value"].'%" ';
$query .= 'OR last_name LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["order"]))
{
$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= 'ORDER BY id DESC ';
}
if($_POST["length"] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row["first_name"];
$sub_array[] = $row["last_name"];
$sub_array[] = '<button type="button" name="update" id="'.$row["id"].'" class="btn btn-warning btn-xs update">Update</button>';
$sub_array[] = '<button type="button" name="delete" id="'.$row["id"].'" class="btn btn-danger btn-xs delete">Delete</button>';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => $data
);
echo json_encode($output);
?>
//javasrcipt jquery
var dataTable = $('#user_data').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"fetch.php",
type:"POST"
},
"columnDefs":[
{
"targets":[0, 3, 4],
"orderable":false,
},
]
});
您还可以检查您的本地网络服务器是否正常工作。只需放置一个文件即可,例如info.php 在 xampp/htdocs 中包含以下内容:
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
在浏览器中用localhost/info.php调用,看看内容是不是这样的:
https://mediatemple.net/community/products/dv/204643880/how-can-i-create-a-phpinfo.php-page
还要检查您的网络服务器是否仍在运行或完全运行。
很好。
var dataTable = $('#user_data').DataTable({
paging: true,
"ajax":{
url:"fetch.php",
type:"POST",
success: function(data, status, xhr) {
// handle success
},
error: function(xhr, status, error) {
// handle error
}
}
})
});
也许分页:true 是解决方案。
404 错误与 ajax 调用有关。我在文档中找到了它。
抱歉,我不熟悉数据表。尝试通过处理程序获得特定的失败。您应该搜索并阅读 https://datatables.net/forums/
它给出:
http://localhost/aps/undefined Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.35 (Win32) OpenSSL/1.1.0i PHP/7.2.11
//The php code to fetch data
<?php
include('db.php');
$query = '';
$output = array();
$query .= "SELECT * FROM users ";
if(isset($_POST["search"]["value"]))
{
$query .= 'WHERE first_name LIKE "%'.$_POST["search"]["value"].'%" ';
$query .= 'OR last_name LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["order"]))
{
$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= 'ORDER BY id DESC ';
}
if($_POST["length"] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row["first_name"];
$sub_array[] = $row["last_name"];
$sub_array[] = '<button type="button" name="update" id="'.$row["id"].'" class="btn btn-warning btn-xs update">Update</button>';
$sub_array[] = '<button type="button" name="delete" id="'.$row["id"].'" class="btn btn-danger btn-xs delete">Delete</button>';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => $data
);
echo json_encode($output);
?>
//javasrcipt jquery
var dataTable = $('#user_data').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"fetch.php",
type:"POST"
},
"columnDefs":[
{
"targets":[0, 3, 4],
"orderable":false,
},
]
});
您还可以检查您的本地网络服务器是否正常工作。只需放置一个文件即可,例如info.php 在 xampp/htdocs 中包含以下内容:
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
在浏览器中用localhost/info.php调用,看看内容是不是这样的: https://mediatemple.net/community/products/dv/204643880/how-can-i-create-a-phpinfo.php-page
还要检查您的网络服务器是否仍在运行或完全运行。
很好。
var dataTable = $('#user_data').DataTable({
paging: true,
"ajax":{
url:"fetch.php",
type:"POST",
success: function(data, status, xhr) {
// handle success
},
error: function(xhr, status, error) {
// handle error
}
}
})
});
也许分页:true 是解决方案。
404 错误与 ajax 调用有关。我在文档中找到了它。
抱歉,我不熟悉数据表。尝试通过处理程序获得特定的失败。您应该搜索并阅读 https://datatables.net/forums/