【问题标题】:JQuery datatable not filling from AJAX call despite successful response尽管响应成功,但 JQuery 数据表未从 AJAX 调用中填充
【发布时间】:2017-07-30 18:40:23
【问题描述】:

我有一个 dot net aspx 页面,其中一个表设置为 JQuery DataTable (https://datatables.net/),使用 AJAX 调用从 Web 方法获取数据。数据作为 JSON 对象返回。

每个部分的机制似乎工作正常。调用 Web 方法并返回一个有效的 JSON 对象(根据 JSONLint),我可以在 AJAX 调用的“成功”回调函数中访问和处理返回的数据。

但是,数据表显示在页面上,没有任何正文行,只有页眉和页脚。

.aspx 页面中的 HTML 部分

<div id="divIssueDetailsTable">
    <table id="tblIssueDetails" class="table">
        <thead>
            <tr>
                <th>Team</th>
                <th>Score</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Team</th>
                <th>Score</th>
            </tr>
        </tfoot>
        <tbody>
        </tbody>
    </table>
</div>

创建数据表的Javascript函数(在成功回调函数中包含测试代码,用于测试返回的JSON对象)

$('#tblIssueDetails').DataTable(
    {
        "processing": true,
        "serverSide": true,
        "cache": false,
        "destroy": true,
        "ajax":
            {
                type: "POST",
                url: "WebServices/WSGetData.asmx/ReturnIssues",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "",
                success: function (results) {
                    toastr["success"]("AJAX call succeeded.", "Success:");
                    var objJSON = $.parseJSON(results.d);
                    $(objJSON.data).each(function () {
                        alert(this.Age);
                    });
                },
                error: function(e) {
                    toastr["error"]("There has been a problem retrieving the information from the database. " + e.responseText, "Error:");
                },
                complete: function () {
                    $('#divIssueDetailsTable').show();
                }
            }
    });

网络方法返回的内容(测试数据)

d: "{ "数据": [{“姓名”:“弗雷迪”,“年龄”:“23”},{“姓名”:“简”,“年龄”:“23”},{“姓名”:“罗德”,“年龄”: “23”}] }"

奇怪的是,所有单独的部分似乎都工作正常,但只是没有刷新表格。

我在数据表上找不到任何链接,只是拒绝显示数据。我尝试了以下方法:

https://datatables.net/examples/basic_init/zero_configuration.html https://datatables.net/examples/data_sources/ajax.html fill datatable with json and web service Datatables not refreshing after second network request datatables dataSrc function not called on successful ajax response

非常感谢任何帮助。

【问题讨论】:

  • 从 $(objJSON.data).each 中删除 .data 会发生什么?试试 $(objJSON).each
  • 这是处理成功函数内部对象的测试代码。如果我删除“.data”,我将在警报框中得到“未定义”。问题是数据表没有显示返回的数据。谢谢。

标签: javascript jquery json ajax datatable


【解决方案1】:

ajax 上的 DataTables 文档说:

success - 必须被覆盖,因为它在 DataTables 内部使用。要操作/转换服务器返回的数据,请使用ajax.dataSrc(上),或使用ajax 作为函数(下)。

通过提供您自己的 success 回调,您剥夺了 DataTables 对 Ajax 结果的访问权限。

您可以改用 dataSrc 回调,它接受服务器响应并且必须返回对象以供 DataTables 使用:

dataSrc: function (results) {
    toastr["success"]("AJAX call succeeded.", "Success:");
    var objJSON = $.parseJSON(results.d);
    return objJSON.data;
},

【讨论】:

  • 关于不覆盖函数的观点 - 谢谢。用于调试的回调函数因此已删除它们并按照建议添加了 dataSrc 函数。现在从数据表中得到一个“未知参数”错误.....我现在可以去调查了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 2011-02-22
  • 2016-06-19
相关资源
最近更新 更多