javascript - 如何在使用数据表发送到模板之前获得 AJAX 响应?

标签 javascript php jquery ajax 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

但它也不会加载数据。

最佳答案

首先,要使用 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"
        ]
    ]
}

这是一个demo

关于javascript - 如何在使用数据表发送到模板之前获得 AJAX 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29089901/

相关文章:

javascript - 如何将数据插入nvd3条形图

javascript - 如何检测麦克风类型

php - 只能发布 2 分钟以内的 YouTube 视频

javascript - 如何使下拉菜单的背景颜色为白色

javascript - 如何使用正则表达式在原始文件中匹配和查找两个字符串之间的整个字符串?

php - MySQL IF 语句?

php - Ruby 符号相当于 PHP 常量吗?

jquery - 为什么当我将显示设置为内联时图像会疯狂增长

javascript - 使用 jQuery 从tinyMCE 编辑器保存数据

javascript - 如何移动到数组的上一个/下一个索引键