【问题标题】:datatables.net data in JSON form not displaying error tn/4JSON 格式的 datatables.net 数据不显示错误 tn/4
【发布时间】:2016-04-22 20:02:53
【问题描述】:

大家好,我有以下 JSON 来自使用 AJAX 的 Web 服务:

{
 "data": [
  [
   "11/11/2014 3:02:37 PM",
   "4/13/2015 8:26:37 AM",
   "032650147",
   "NULL",
   "Web Site Problems",
   "NULL",
   "New",
   "6230.758742407"
  ],
  [
   ...etc etc....
  ]
 ]
}

我正在动态创建 HTML 表格,如下所示:

<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
     <tr>
        <th>Contact Date Time</th>
        <th>Last Update Date Time</th>
        <th>Member ID</th>
        <th>Operator NTID</th>
        <th>Question</th>
        <th>Redirect Email Address</th>
        <th>Status</th>
        <th>Receipt Date</th>
      </tr>
   </thead>
</table>

而我用来在表中显示返回数据的 JavaScript 是:

$.ajax({
   type: "POST",
   crossDomain: true,
   url: "complete.aspx/getMemberEmailsDBData",
   beforeSend: function (xhrObj) {
       xhrObj.setRequestHeader("Content-Type", "application/json");
   },
   data: {},
   dataType: "json",
   success: function (data) {
        console.log(data.d);
        $('#example').DataTable({
           data: data.d,
           columns: [
              { title: "ContactDateTime" },
              { title: "LastUpdateDateTime" },
              { title: "MemberID" },
              { title: "OperatorNTID" },
              { title: "QuestionArea" },
              { title: "RedirectEmailAddress" },
              { title: "Status" },
              { title: "ReceiptDate" }
           ] 
        });
   },
   error: function (XMLHttpRequest, textStatus, errorThrown) {
       console.log(XMLHttpRequest);
   }
});

加载页面后,它不断给我弹出警报,告诉我:

DataTables 警告:表 id=example - 请求的未知参数“1” 对于第 0 行第 1 列。有关此错误的更多信息,请参阅 http://datatables.net/tn/4.

有人看到我缺少的东西来解决这个问题吗?

【问题讨论】:

  • console.log(data.d); output 是什么意思?
  • 我在上面的 OP 中发布的 JSON。

标签: jquery json ajax vb.net datatables


【解决方案1】:

使用

data: data.d.data, 

当您使用 javascript array source 时,dataTables 确实期望将源包装到 data : [ ... ] 中,而只是一个数组数组。

小演示中的代码 -> http://jsfiddle.net/jusbngww/

【讨论】:

  • 如果你尝试console.log(data.d.data),它会输出array[x]?此时,您肯定应该拥有格式良好的 JSON。
  • 我在使用 console.log(data.d.data) 时得到一个 undefined
  • @StealthRT,实际上,您甚至不确定您的回复中有有效的 JSON 或 d 吗?你试过data = JSON.parse(data)之类的吗?
猜你喜欢
  • 1970-01-01
  • 2014-03-30
  • 2015-05-05
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
相关资源
最近更新 更多