DataTables使用您自己的参数读取ajax响应 [英] DataTables read ajax response with your own parameters

查看:97
本文介绍了DataTables使用您自己的参数读取ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器端模式下使用DataTables 1.10.15.我创建了一个PHP脚本来提供JSON响应,其中包括它们在文档中提到的参数:

Using DataTables 1.10.15 in Server Side mode. I've created a PHP script to provide a JSON response which includes the parameters they mention in the documentation: https://datatables.net/manual/server-side#Returned-data

我想将自己的参数添加到JSON响应中,例如

I want to add my own parameters to the JSON response, e.g.

$response = [
    'data' => [ ], // Required by DataTables 
    'form_errors' => [ ] // Not required by DataTables
];
echo json_encode($response);

我为ajax调用准备的js看起来像这样:

The js which I have for the ajax call looks like this:

var myTable = $('#myTable').DataTable( {
    "serverSide": true,
    "ajax": { 
        "url" : "/response.php",
        "method" : "POST"
    },
});

我如何阅读ajax响应?我在API中看到有一个.on('xhr')方法( https://datatables.net/reference/event/xhr )在ajax请求完成时触发,例如

How can I read the ajax response? I've seen in the API that there is a .on('xhr') method (https://datatables.net/reference/event/xhr) which fires when the ajax request has been completed, e.g.

var myTable = $('#myTable').DataTable( {
    "serverSide": true,
    "ajax": { 
        "url" : "/response.php",
        "method" : "POST"
    },
}).on( 'xhr.dt', function () {
    // Read response here?
});

但是我当时找不到找到ajax响应数据的方法.

But I cannot find a way to read the ajax response data at that point.

有人知道这是否可能吗?

Does anyone know if this is possible?

推荐答案

旧问题,但我会尝试回答将来可能需要的问题,因为我遇到了完全相同的问题,并且在查找了他们的文档后发现 drawCallback .

Old question, but i'll try to answer may need for future, because i have exactly same problem and after looking for their documentation i found drawCallback.

根据您的代码:

var myTable = $('#myTable').DataTable( {
    "serverSide": true,
    "ajax": { 
        "url" : "/response.php",
        "method" : "POST"
    },
    "drawCallback": function (settings) { 
        // Here the response
        var response = settings.json;
        console.log(response);
    },
});

这篇关于DataTables使用您自己的参数读取ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆