来自php DataTables的Ajax警告:table id = example - 无效的JSON响应

时间:2015-09-24 03:33:31

标签: 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返回,有任何错误吗?

1 个答案:

答案 0 :(得分:0)

this page in the documentation上,它说明了以下内容:

  

来自服务器的回复

     

在回复DataTables向服务器发出的每个信息请求时,它希望得到一个结构良好的JSON对象,其中包含以下参数....

enter image description here

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