【问题标题】:Problem with datatable ajax pagination, it give undefined url数据表ajax分页问题,​​它给出未定义的url
【发布时间】:2021-06-25 17:38:42
【问题描述】:

它给出:

http://localhost/aps/undefined 找不到对象!在此服务器上找不到请求的 URL。引用页面上的链接似乎是错误的或过时的。请将该错误通知该页面的作者。如果您认为这是服务器错误,请联系网站管理员。错误 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,
            },
        ]

    });

【问题讨论】:

  • 也许这个链接对你有帮助:stackoverflow.com/questions/15046543/…你检查文件是否在你本地网络服务器的 htdocs 文件夹中的正确位置
  • 不,我的 ht 文档是 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php [NC,L] php_flag register_globals 0 php_flag magic_quotes_gpc 0 php_flag magic_quotes_runtime 0
  • 不是 .htaccess 设置。我的意思是,如果文件在物理上位于 xampp/htdocs/aps/yourfile.php 之类的文件夹中,这是不同的。

标签: php jquery ajax datatable


【解决方案1】:

您还可以检查您的本地网络服务器是否正常工作。只需放置一个文件即可,例如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
         }
        } 
    })
});

也许 paging: true 是解决方案。

404 错误与 ajax 调用有关。我在文档中找到了它。 抱歉,我不熟悉 DataTables。尝试使用处理程序获得特定的失败。你应该搜索并阅读https://datatables.net/forums/

【讨论】:

  • 我的网络应用程序位于 htdocs 将所有页面中,显示数据表的页面也在同一文件夹中,即 localhost/aps/datatable.php 和表格显示以及但出现问题当我点击分页链接时,他们给出的新页面是 localhost/aps/undefined 有什么问题?
  • 我添加了分页:是的,它工作@Bernhard Beatus,js文件末尾也有一些错误请看上面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 2014-01-08
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
相关资源
最近更新 更多