【问题标题】:Datatable binds only half of the dataDatatable 只绑定了一半的数据
【发布时间】:2018-07-10 15:16:39
【问题描述】:

我正在尝试将数据绑定到 jQuery 数据表,但它只绑定了其中的一半。我检查了 AJAX 调用;它返回成功,我正在正确获取数据。但是,相同的数据没有绑定到网格。

我的数据:

{
  "draw": "1",
  "recordsFiltered": 1,
  "recordsTotal": 1,
  "data": [{
    "TitleName": "w",
    "CustomerName": "q",
    "ServiceType": "r",
    "MailClass": "w",
    "ProcessingCategory": "a",
    "Origin": "a",
    "IsActive": true,
    "DateModified": "1/31/2018 12:00:00 AM",
    "ContentTitleId ": 1,
    "MailClassId ": 1,
    "ProcessingCategoryId ": 1,
    "ServiceTypeId ": 1,
    "TransportationTypeId ": 1
  }]
}
$(document).ready(function () {
  $('#example').DataTable({
    processing: true,
    serverSide: true,
    ajax: {
      url: "/ContentTitleManagement/OverviewPageWithServerSidePagination",
      type: "GET",
      datatype: "json",
      // success: function (result) {
      //   console.log("something");
      // }
    },
    columns: [
      { "data": "TitleName" },
      { "data": "CustomerName" },
      { "data": "ServiceType" },
      { "data": "MailClass" },
      { "data": "ProcessingCategory" },
      { "data": "Origin" },
      { "data": "DateModified" },
      { "data": "IsActive" }, 
      // Data gets bound up to here
      // { "data": "ContentTitleId" },
      // { "data": "MailClassId" },
      // { "data": "ProcessingCategoryId" },
      // { "data": "ServiceTypeId" },
      // { "data": "TransportationTypeId" }
    ]
  });
});
<table id="example">
  <thead>
    <tr>
      <th>TITLE NAME</th>
      <th>CUSTOMER NAME</th>
      <th>SERVICE<br />TYPE</th>
      <th>MAIL<br />CLASS</th>
      <th>PROCESSING<br />CATEGORY</th>
      <th>ORIGIN</th>
      <th>DATE<br />MODIFIED</th>
      <th>IS<br />ACTIVE</th>
      <th>Content Title Id</th>
      <th>Mail Class Id</th>
      <th>Processing Category Id</th>
      <th>Service Type Id</th>
      <th>Transportation Type Id</th>
    </tr>
  </thead>
</table>

只要我在columns 方法的columns 部分中注释这些列,上面的代码就可以工作文件。但是,如果我取消注释,则会引发以下错误

DataTables 警告:table id=example - Ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7

【问题讨论】:

  • 错误链接的 URL 告诉您所有您需要知道的信息:This occurs when jQuery falls into its error callback handler (this callback built into DataTables), which will typically occur when the server responds with anything other than a 2xx HTTP status code. 您的 AJAX 请求导致服务器端错误。您需要调试为什么会发生这种情况
  • @RoryMcCrossan 没有服务器端错误。因为如果我调用 URL localhost:8309/ContentTitleManagement/…,我会获取数据
  • @RoryMcCrossan 和我仍然可以将一半数据与注释掉的列绑定。只有当我取消注释这些列时才会导致问题
  • 那么您确定问题中包含的错误是由您当前的代码还是它的缓存版本产生的?
  • @RoryMcCrossan 尝试在隐身模式下运行,甚至我在运行前清理了浏览器。 + 我发现如果我取消注释这些列,脚本甚至不会进行 ajax 调用。但是我看到js是正确的。

标签: javascript jquery json ajax datatables


【解决方案1】:

数据中最后 5 个标识符中有空格:

"IsActive": true,
"DateModified": "1/31/2018 12:00:00 AM",
"ContentTitleId ": 1,  <-- space at the end
"MailClassId ": 1,
"ProcessingCategoryId ": 1,
"ServiceTypeId ": 1,
"TransportationTypeId ": 1

您必须将其更改为:

"IsActive": true,
"DateModified": "1/31/2018 12:00:00 AM",
"ContentTitleId": 1,
"MailClassId": 1,
"ProcessingCategoryId": 1,
"ServiceTypeId": 1,
"TransportationTypeId": 1

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 2020-08-18
    • 1970-01-01
    • 2015-06-07
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多