如何在使用 DATATABLES 发送到模板之前获取 AJAX 响应

How to get AJAX response before sending to template using DATATABLES?

本文关键字:获取 响应 AJAX DATATABLES      更新时间:2023-09-26

我正在管理面板中实现一个日志表,我使用了数据表插件。我在数据表中创建了一个 ajax,但我不知道如何在发送到表之前获得响应。

我的jquery中有这个:

<script type="text/javscript">
    $(document).ready(function() {
        $('#log-pageview')
            .on('xhr .dt', function(e, settings, json) {
                $('#status').html(json.status)
            })
            .dataTable({
            "processing": true,
            "serverSide": true,
            "ajax": "/secure/logs/visits.php?log_get=1"
        });
    });
</script>

在我的 HTML 中,我有这个:

<table id="log-pageview" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Log ID</th>
            <th>User ID</th>
            <th>IP Address</th>
            <th>Date Viewed</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Log ID</th>
            <th>User ID</th>
            <th>IP Address</th>
            <th>Date Viewed</th>
        </tr>
    </tfoot>
</table>
<div id="status"></div>

在我的服务器端PHP中,我有这个。

function get_pageview() {
    $host                   =   mysql_connect('localhost','avjunky_2','1qaz2wsx') or die(mysql_error());
    mysql_set_charset('utf8', $host); //added for character encoding, made it global for functions /** added by rochelle **/
    $db                     =   mysql_select_db('avjunky_2',$host) or die(mysql_error());
    $log_array = array();
    $log_data = array();
    $get_all_logs = mysql_query("SELECT id, logged_id, ip_address, date_viewed FROM avjunky_pageview", $host);
    while($row_logs = mysql_fetch_array($get_all_logs)) {
        $log_data[] = array(
            'id'                    =>  $row_logs['id'],
            'logged_id'     =>  $row_logs['logged_id'],
            'ip_address'    =>  $row_logs['ip_address'],
            'date_viewed'   =>  $row_logs['date_viewed']
        );
    }
    $total_count = count($log_data);
    $log_array = array(
        'draw'                  =>  1,
        'recordsTotal'  =>  $total_count,
        'recordsFiltered' =>    $total_count,
        'data'                  =>  $log_data
    );
    echo json_encode($log_array);
}   
if(isset($_GET['log_get'])) {
    get_pageview();
}

在服务器端,我生成了这种 json:

{"draw":1,"recordsTotal":2,"recordsFiltered":2,"data":[{"id":"3","logged_id":"7","ip_address":"122.2.55.11","date_viewed":"2015-03-16 10:10:42"},{"id":"2","logged_id":"8","ip_address":"122.2.55.11","date_viewed":"2015-03-17 00:05:40"}]}

{
    "draw":1,
    "recordsTotal":2,
    "recordsFiltered":2,
    "data":[
        {
            "id":"3",
            "logged_id":"7",
            "ip_address":"122.2.55.11",
            "date_viewed":"2015-03-16 10:10:42"
        },
        {
            "id":"2",
            "logged_id":"8",
            "ip_address":"122.2.55.11",
            "date_viewed":"2015-03-17 00:05:40"
        }
    ]
}

我不知道我哪里做错了。我检查了开发人员工具网络选项卡,似乎 ajax 没有调用 URL。

在我的 AJAX 中,我还尝试像这样调用来自 URL 的响应。

http://mysite/admin/secure/logs/visits.php?log_get=1

但它也不会加载数据。

首先,要使用xhr.dt数据表,应该使用1.10>。

其次,数据应具有以下结构。

{
    "draw":1,
    "recordsTotal":2,
    "recordsFiltered":2,
    "data":[
        [
            "3",
            "7",
            "122.2.55.11",
            "2015-03-16 10:10:42"
        ],
        [
            "2",
            "8",
            "122.2.55.11",
            "2015-03-17 00:05:40"
        ]
    ]
}

这是一个演示