javascript - 来自 php DataTables 的 Ajax 警告 :table id=example - Invalid JSON response

标签 javascript php jquery json ajax

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="GBK">
        <title></title>
    </head>
    <body>
           {"sEcho":1,"iTotalRecords":1,"iTotalDisplayRecords":1,"aaData":[["086671","NB","MSC-1","09-APR-15"],["086673","DB","MSC-2","09-APR-15"],["086678","DA","MSC-1","10-APR-15"],["086682","DA","MSC-1","10-APR-15"],["086683","NA","MSC-1","10-APR-15"],["086491","NA","MSC-2","25-MAR-15"],["086686","DA","MSC-1","10-APR-15"],["086688","DA","MSC-2","10-APR-15"],["086690","NA","MSC-2","10-APR-15"],["086496","DA","MSC-1","25-MAR-15"],["086685","NB","MSC-1","09-APR-15"],["086848","NA","MSC-2","22-APR-15"],["086516","NA","MSC-1","22-APR-15"],["086523","DA","MSC-1","22-APR-15"],["086839","DA","MSC-2","22-APR-15"],["086849","DA","MSC-1","22-APR-15"],["086528","NB","MSC-1","24-APR-15"],["086526","NB","MSC-1","27-MAR-15"],["086478","DA","MSC-1","25-MAR-15"],["086482","DB","MSC-1","27-MAR-15"],["086832","NB","MSC-1","24-APR-15"],["086840","DB","MSC-2","24-APR-15"],["086842","NB","MSC-2","24-APR-15"],["086828","DB","MSC-1","24-APR-15"],["086987","NB","MSC-1","07-MAY-15"],["086991","DB","MSC-2","07-MAY-15"]]}    </body>
</html>

我是 php 和 jQuery 的新手,现在我有一个问题;当我使用数据表查询数据时,错误为标题,详细代码如下:

html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="GBK">
            <title>作业</title>
            <link href="http://CEA815099W/test/cim_web/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
            <link rel="stylesheet" type="text/css" href="http://Cea815099w/test/cim_web/css/dataTables.bootstrap.css" />
            <script src="http://CEA815099W/test/cim_web/js/jquery-1.11.3.min.js"></script>
            <script src="http://CEA815099W/test/cim_web/js/jquery.dataTables.min.js"></script>
            <script src="http://CEA815099W/test/cim_web/js/dataTables.bootstrap.js"></script>
            <script src="http://CEA815099W/test/cim_web/js/echarts.js"></script>
            <script src="http://CEA815099W/test/cim_web/js/bootstrap.min.js">
    </head>
    <body>
  <!-- table--> 
  <div class="container">
    <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
              <tr>
                  <th>工号</th>
                  <th>班别</th>
          <th>站点</th>
                  <th>时间</th>
              </tr>
          </thead>
          <tbody>
          <!-- js处理数据库内容--> 
          </tbody>
          <tfoot>
              <tr>
                  <th>工号</th>
                  <th>班别</th>
          <th>站点</th>
                  <th>时间</th>
              </tr>
          </tfoot>  
     </table>
  </div>
</body>
</html>

Javascript:

$(document).ready(function() {
       $('#example').dataTable( {
           "bProcessing": true,
           "sAjaxSource": "data.php"
       } );
   } );    

</script>
<script type="text/javascript">
    // For demo to fit into DataTables site builder...
    $('#example')
           .removeClass( 'display' );
            .addClass('table table-striped table-bordered');
</script>

PHP:

<?php
$db = new PDO("oci:dbname=mfdm10", "f10mfg",'demon');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);  // 设置为警告模式
$rs = $db->prepare("select a.empid,a.shift,a.station,a.infab_time from ut_msc_newcoming a where rownum <= :num"); // SQL 需要修改
$rs->bindValue(':num', 26);
$rs->execute();
while($row=$rs->fetch()){       
    $data[]=  array(
        $row[0],
        $row[1],
        $row[2],
        $row[3]
              );
    }

/* $output = array(
        "sEcho" => 1,
        "iTotalRecords" => count($row),
        "iTotalDisplayRecords" => count($row),
        "aaData" => $data
    );  */
    $response = array();
    $response['success'] = true;
    $response['aaData'] = $data;
    echo json_encode( $response );

    //异常
    function fatal($msg)
    {
        echo json_encode(array(
            "error" => $msg
        ));
        exit(0);
    }

?>

添加我从控制台网络返回的json

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="GBK">
        <title></title>
    </head>
    <body>
           {"success":true,"aaData":[["086671","NB","MSC-1","09-APR-15"],["086673","DB","MSC-2","09-APR-15"],["086678","DA","MSC-1","10-APR-15"],["086682","DA","MSC-1","10-APR-15"],["086683","NA","MSC-1","10-APR-15"],["086491","NA","MSC-2","25-MAR-15"],["086686","DA","MSC-1","10-APR-15"],["086688","DA","MSC-2","10-APR-15"],["086690","NA","MSC-2","10-APR-15"],["086496","DA","MSC-1","25-MAR-15"],["086685","NB","MSC-1","09-APR-15"],["086848","NA","MSC-2","22-APR-15"],["086516","NA","MSC-1","22-APR-15"],["086523","DA","MSC-1","22-APR-15"],["086839","DA","MSC-2","22-APR-15"],["086849","DA","MSC-1","22-APR-15"],["086528","NB","MSC-1","24-APR-15"],["086526","NB","MSC-1","27-MAR-15"],["086478","DA","MSC-1","25-MAR-15"],["086482","DB","MSC-1","27-MAR-15"],["086832","NB","MSC-1","24-APR-15"],["086840","DB","MSC-2","24-APR-15"],["086842","NB","MSC-2","24-APR-15"],["086828","DB","MSC-1","24-APR-15"],["086987","NB","MSC-1","07-MAY-15"],["086991","DB","MSC-2","07-MAY-15"]]}    </body>
</html>

我原来的json返回,有错误吗?

最佳答案

关于this page in the documentation它指出以下内容:

Reply from the server

In reply to each request for information that DataTables makes to the server, it expects to get a well formed JSON object with the following parameters....

enter image description here

您似乎没有设置这些变量,这可能是错误消息“警告:表 id=example - 无效的 JSON 响应”的原因,因为您的 JSON 虽然有效,但不是有效的“JSON 响应”与数据表期望的内容相关。

关于javascript - 来自 php DataTables 的 Ajax 警告 :table id=example - Invalid JSON response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752875/

相关文章:

javascript - 您可以使用脚本的替代来源吗?

javascript - 我如何在从表单获取用户输入的对象数组内部设置状态

javascript - 带有角色/带有meteor-roles包的组的组层次结构

php - 使用 ffmpeg 编码时播放视频

javascript - 具有多个按钮的 jQuery 模态表单

javascript - jQuery 选择器错误 : unrecognized expression

php - Codeigniter 405方法不允许AJAX JQuery

javascript - 如何在 Typescript 中从 request.body 检查对象类型?

php - Laravel 5.2 中的正则表达式验证

javascript - 如何借助 jQuery 拒绝容器中的可放置元素?