数据表警告:Json 响应不起作用

Posted

技术标签:

【中文标题】数据表警告:Json 响应不起作用【英文标题】:DataTables warning: Json Response not working 【发布时间】:2019-12-02 11:26:18 【问题描述】:

我正在尝试使用 IDcompany 下拉列表过滤我的表,它适用于我的所有选项,但出现以下消息的选项除外:

DataTables 警告:表 id=empTable - JSON 响应无效。有关此错误的更多信息,请参阅http://datatables.net/tn/1

我检查了调试器网络选项卡中的 json 响应,但那里没有列出任何内容。我认为问题出在我的查询上,但是我回应了它,并且如果我在 phpmyadmin 中编写它,它是正确构造和工作的。

我在容器中有完全相同的代码,它工作得非常好,但在我的生产服务器中它存在上述问题。当我为状态添加特定过滤器时,虽然它可以正常工作,但仅适用于该特定过滤器。

我如何创建查询:

(它适用于其他一切)

$empQuery = "select t.ID, c.name as IDcompany, v.name as IDvessel, w.name as IDwarehouse,
        t.orderno, s.name as IDstatus, su.name as IDsupplier, t.warehouseno, t.orderdate,
        t.datereceived, t.packages, t.weight, t.dimensions, r.name as shippingremarks,
        dan.name as IDdangerous, t.transportorderno, t.pacorefno, t.datedeliveredtovessel,
        t.destination, t.methodofdelivery, t.edt, t.description, t.description_color,
        t.orderdue, t.document, grp.groups
    from transactions t
    left join vessels v on t.IDvessel=v.ID
    left join companies c on t.IDcompany=c.ID
    left join statuses s on t.IDstatus=s.ID
    left join suppliers su on t.IDsupplier=su.ID
    left join warehouses w on t.IDwarehouse=w.ID
    left join remarks r on t.shippingremarks=r.ID
    left join yesno dan on t.IDdangerous=dan.ID
    left join admin_groups grp on t.groups=grp.ID
    WHERE t.deleted = 'No' "
    .$admQuery
    .$searchQuery
    ." order by ".$columnName." ".$columnSortOrder
    ." limit ".$row.",".$rowperpage; 

我如何创建我的 json 请求:

$(document).ready(function()
      var dataTable = $('#empTable').DataTable(

          'processing': true,
          'serverSide': true,
          'serverMethod': 'post',
          //-- to remove the extra filter --
          'bFilter': false,
           //-- to move the sorting icon in the first row of the thead in the table --
          'bSortCellsTop': true,
          //-- to remove the sorting in action column --
          'columnDefs': [
            'orderable': false,
            'targets': [0,1,25]
            ],

          // "scrollX": false,

          //'bAutoWidth': false,
          // 'fixedColumns': true,
          'oLanguage': 
            'oPaginate': 
              'sNext': 'Next'
            
          ,
          "aaSorting": [[2,'asc']],
          'async': false,
          "pageLength": 50,

          'ajax': 
              'url':'ajaxfile.php',
              // 'async':true,
              'data': function(data)
                  // Read values
                  var company = $('#searchByCustomer').val();
                  var vessel = $('#searchByVessel').val();
                  var warehouse = $('#searchByWarehouse').val();
                  var orderno = $('#searchByOrderNo').val();
                  var status = $('#searchByStatus').val();
                  var supplier = $('#searchBySupplier').val();
                  var warehouseno = $('#searchByStockNo').val();
                  var orderdate = $('#searchByOrderDate').val();
                  var datereceived = $('#searchByDateReceived').val();
                  var packages = $('#searchByCLL').val();
                  var weight = $('#searchByWeight').val();
                  var dimensions = $('#searchByPacoEyes').val();
                  var shippingremarks = $('#searchByRemark').val();
                  var IDdangerous = $('#searchByDGR').val();
                  var transportorderno = $('#searchByTransportNo').val();
                  var pacorefno = $('#searchByPacoRef').val();
                  var datedeliveredtovessel = $('#searchByDateDelivered').val();
                  var destination = $('#searchByDeliveredAt').val();
                  var methodofdelivery = $('#searchByMethodOfDelivery').val();
                  var edt = $('#searchByEstimatedDelivery').val();
                  var description = $('#searchByDescription').val();
                  var description_color = $('#searchByColor').val();
                  var orderdue = $('#searchByValue').val();
                  var groups = $('#searchByGroup').val();

                  // Append to data
                  data.searchByCustomer = company;
                  data.searchByVessel = vessel;
                  data.searchByWarehouse = warehouse;
                  data.searchByOrderNo = orderno;
                  data.searchByStatus = status;
                  data.searchBySupplier = supplier;
                  data.searchByStockNo = warehouseno;
                  data.searchByOrderDate = orderdate;
                  data.searchByDateReceived = datereceived;
                  data.searchByCLL = packages;
                  data.searchByWeight = weight;
                  data.searchByPacoEyes = dimensions;
                  data.searchByRemark = shippingremarks;
                  data.searchByDGR = IDdangerous;
                  data.searchByTransportNo = transportorderno;
                  data.searchByPacoRef = pacorefno;
                  data.searchByDateDelivered = datedeliveredtovessel;
                  data.searchByDeliveredAt = destination;
                  data.searchByMethodOfDelivery = methodofdelivery;
                  data.searchByEstimatedDelivery = edt;
                  data.searchByDescription = description;
                  data.searchByColor = description_color;
                  data.searchByValue = orderdue;
                  data.searchByGroup = groups;
              
          ,
          'columns': [
               data: 'action' ,
               data: 'check',
               data: 'IDcompany' ,
               data: 'IDvessel' ,
               data: 'IDwarehouse' ,
               data: 'orderno' ,
               data: 'IDstatus' ,
               data: 'IDsupplier' ,
               data: 'warehouseno' ,
               data: 'orderdate' ,
               data: 'datereceived' ,
               data: 'packages' ,
               data: 'weight' ,
               data: 'dimensions' ,
               data: 'shippingremarks',
               data: 'IDdangerous' ,
               data: 'transportorderno' ,
               data: 'pacorefno' ,
               data: 'datedeliveredtovessel' ,
               data: 'destination' ,
               data: 'methodofdelivery' ,
               data: 'edt' ,
               data: 'description' ,
               data: 'description_color' ,
               data: 'orderdue' ,
               data: 'document' ,
               data: 'groups' ,

          ]
      );


      $('#searchByCustomer').change(function()
          dataTable.draw();
      );
      $('#searchByVessel').change(function()
          dataTable.draw();
      );
      $('#searchByWarehouse').change(function()
          dataTable.draw();
      );
      $('#searchByOrderNo').keyup(function()
          dataTable.draw();
      );
      $('#searchByStatus').change(function()
          dataTable.draw();
      );
      $('#searchBySupplier').change(function()
          dataTable.draw();
      );
      $('#searchByStockNo').keyup(function()
          dataTable.draw();
      );
      $('#searchByOrderDate').keyup(function()
          dataTable.draw();
      );
      $('#searchByDateReceived').keyup(function()
          dataTable.draw();
      );
      $('#searchByCLL').keyup(function()
          dataTable.draw();
      );
      $('#searchByWeight').keyup(function()
          dataTable.draw();
      );
      $('#searchByPacoEyes').keyup(function()
          dataTable.draw();
      );
      $('#searchByRemark').change(function()
          dataTable.draw();
      );
      $('#searchByDGR').change(function()
          dataTable.draw();
      );
      $('#searchByTransportNo').keyup(function()
          dataTable.draw();
      );
      $('#searchByPacoRef').keyup(function()
          dataTable.draw();
      );
      $('#searchByDateDelivered').keyup(function()
          dataTable.draw();
      );
      $('#searchByDeliveredAt').keyup(function()
          dataTable.draw();
      );
      $('#searchByMethodOfDelivery').keyup(function()
          dataTable.draw();
      );
      $('#searchByEstimatedDelivery').keyup(function()
          dataTable.draw();
      );
      $('#searchByDescription').keyup(function()
          dataTable.draw();
      );
      $('#searchByColor').change(function()
          dataTable.draw();
      );
      $('#searchByValue').keyup(function()
          dataTable.draw();
      );

      $('#searchByGroup').change(function()
          dataTable.draw();
      );
      $("#actionReset").bind("click", function() 
          $("input[type=text]").val("");
          $("input[type=checkbox]").val("");
          $("#searchByGroup").val("");
          $("#searchByCustomer").val("");
          $("#searchByVessel").val("");
          $("#searchByWarehouse").val("");
          $("#searchByStatus").val("");
          $("#searchBySupplier").val("");
          $("#searchByRemark").val("");
          $("#searchByDGR").val("");
          $("#searchByColor").val("");

          dataTable.draw();
      );
  );

$admQuery

if($admGroup == 'admin')
   $admQuery="";
else if ($admGroup == 'hamburg') 
   $admQuery=" and (grp.groups = '".$admGroup."' 
   or grp.groups = 'common') ";

$searchQuery

if($searchByCustomer != '')
  $searchQuery .= " and (t.IDcompany= '".$searchByCustomer."' ) ";

if($searchByVessel != '')
  $searchQuery .= " and (t.IDvessel='".$searchByVessel."' ) ";

if($searchByWarehouse != '')
  $searchQuery .= " and (t.IDwarehouse='".$searchByWarehouse."' ) ";

if($searchByOrderNo != '')
  $searchQuery .= " and (t.orderno like '%".$searchByOrderNo."%' ) ";

...

这是一个呈现的 mysql 查询示例:

select t.ID, c.name as IDcompany, v.name as IDvessel, w.name as IDwarehouse, t.orderno,
       s.name as IDstatus, su.name as IDsupplier, t.warehouseno, t.orderdate,
       t.datereceived, t.packages, t.weight, t.dimensions, r.name as shippingremarks,
       dan.name as IDdangerous, t.transportorderno, t.pacorefno, t.datedeliveredtovessel,
       t.destination, t.methodofdelivery, t.edt, t.description, t.description_color,
       t.orderdue, t.document, grp.groups
from transactions t
left join vessels v on t.IDvessel=v.ID
left join companies c on t.IDcompany=c.ID 
left join statuses s on t.IDstatus=s.ID 
left join suppliers su on t.IDsupplier=su.ID 
left join warehouses w on t.IDwarehouse=w.ID 
left join remarks r on t.shippingremarks=r.ID 
left join yesno dan on t.IDdangerous=dan.ID
left join admin_groups grp on t.groups=grp.ID
WHERE t.deleted = 'No'
  and (grp.groups = 'hamburg' or grp.groups = 'common')
  and (t.IDcompany= '84' )
  and (t.IDstatus='3' )
order by IDcompany asc 
limit 0,50

这是我得到的,但 Ajax 响应作为错误消息返回。

【问题讨论】:

返回的错误信息是什么? 错误是:DataTables 警告:table id=empTable - JSON 响应无效。有关此错误的更多信息,请参阅datatables.net/tn/1 显示更多代码..这个json是如何生成的? datatables.net/manual/tech-notes/7 我需要看看$admQuery$searchQuery 是如何定义的。当没有“过滤器”时,您可能有一个流氓and 被写入您的查询。整数值不需要在 mysql 中引用。 ...erm,您正在使用准备好的语句,对吗? 【参考方案1】:

我已经解决了这个问题。这不是服务器端或 ajax 问题,而是数据库问题。某些记录包含特殊字符,例如“-”(德语破折号),查询无法正确读取并导致错误。

我用正常的破折号“-”替换了这些记录,它工作正常。

【讨论】:

以上是关于数据表警告:Json 响应不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Angular:Typescript 将 JSON 响应转换为对象模型不起作用

访问 JSON 响应中的特定元素(通常的方法不起作用)

快速将 REST API 中的 JSON 数据填充到表格视图中不起作用

Yii2 - JSONP 响应不返回任何内容(空白)并且不起作用,但 JSON 可以

我正在尝试使用 Jquery-Ajax 使用 JSON 响应,但它不起作用。正在加载空选择下拉列表

Express 4 - 用 promise.then 链接 res.json 不起作用