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

时间:2015-03-17 01:33:25

标签: javascript php jquery ajax datatable

我在管理面板中实现了一个日志表,并使用了datatable插件。 我在我的数据表中创建了一个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

但它也不会加载数据。

1 个答案:

答案 0 :(得分:2)

首先,使用xhr.dt数据表&gt;应该使用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"
        ]
    ]
}

这是demo