繁体   English   中英

jQuery DataTables:数据未通过Ajax显示在表中

[英]Jquery DataTables: Data not displaying in table via Ajax

我的问题是Jquery DataTables挂在Loading上,不会显示来自php脚本的任何数据:

在此处输入图片说明

这是我的HTML:

 <!-- Default box -->
  <div class="box">
    <div class="box-header with-border">
      <h3 class="box-title">Time Management</h3>

      <div class="box-tools pull-right">
        <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
          <i class="fa fa-minus"></i></button>
        <button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
          <i class="fa fa-times"></i></button>
      </div>
    </div>
    <div class="box-body">
       <table id="example" class="table table-bordered table-hover">
           <thead>
               <tr>
               <th>ID</th>
               <th>Clock In</th>
               <th>Lunch Started</th>
               <th>Lunch Ended</th>
               <th>Clock Out</th>
               </tr>
           </thead>
       </table>
    </div>
    <!-- /.box-body -->
    <div class="box-footer">
      Footer
    </div>
    <!-- /.box-footer-->
  </div>
  <!-- /.box -->

我的Jquery代码:

<script>
$(document).ready(function() {
$('#example').DataTable({
   "ajax": "api/timemanageprocess.php", 
   "dataSrc": '',
   "type": "POST",

"columns": [
    {"data": "PASS_ID"},
    {"data": "CLOCK_IN"},
    {"data": "START_LUNCH"},
    {"data": "END_LUNCH"},
    {"data": "CLOCK_OUT"}
    ],
  });
});
</script>

而我的PHP脚本的结果。 我回显了json_encode()的结果: 在此处输入图片说明

我试图使用"data": data & "data": json作为DataTable()中的选项。 我试图用大括号定义ajax的选项。 我试着完全排除dataSrc='' ,我试着删除type: 'POST'并将其留给GET 我知道我的PHP脚本地址正确。 我不知道它是什么,或者它们是阻止数据填充到数据表中的原因。 有人可以帮我解决吗? 提前致谢。 帮助将不胜感激。

PHP:

include ('../includes/sessions.php');

$select = "SELECT PASS_ID, CLOCK_IN, START_LUNCH, END_LUNCH, CLOCK_OUT FROM timeclock WHERE USERNAME = '$sessuser'";

 $query = mysqli_query($dbc, $select) or die(mysqli_error($dbc));
 $resnum = mysqli_num_rows($query);

 //echo $resnum;
 while($row = $query->fetch_assoc()){

  $out[] = $row;

  }
  echo json_encode(array('data' => $out));
  mysqli_free_result($query);
  $dbc->close();
  ?>

按照Louys Patrice Bessette的建议,在PHP方面,我将数组改为json对象,而不是json数组。 我,然后我取消报价选项。 所以代替:

$('#example').DataTable({
   "ajax": "url", 
   "columns" : [
    {"data": "keyname"},
     {"data": "keyname"},
    ]
}) 

我做了:

   $('#example').DataTable({
   ajax: "url", 
   columns : [
     {"data": "keyname"},
     {"data": "keyname"},
    ]
})

我也删除了dataSrc选项。

在收到的json中,没有data属性。

如果您可以修改json的生成方式,请将所有对象放置在data对象中...就像其文档中提供的示例一样:

{
  "data": [
    {
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
    /...

或者,如果您无法修改json,请尝试以下操作:( 我没有对此进行测试...您必须尝试)

  "columns": [
    {"PASS_ID"},
    {"CLOCK_IN"},
    {"START_LUNCH"},
    {"END_LUNCH"},
    {"CLOCK_OUT"}
    ],
  });


编辑

只是为了确保json没有问题...
尝试从另一个ajax请求中请求它(仅作为测试):

 $.ajax({ url: "api/timemanageprocess.php", method: "post", dataType: "json", success: function(response){ console.log( JSON.stringify(response) ); }, error: function(request, status, errorThrown){ console.log(errorThrown); } }); 

如果结果正确,我们将围绕Datatable进行调查。
想法是缩小问题...
;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM