doz22551 2015-09-24 03:33
浏览 380

来自php DataTables的Ajax警告:table id = example - 无效的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>
           {"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>

I am a new-comer to php and jQuery, now I have one question; when I use a datatable to query data, error as title, and detail code as follows:

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);
    }

?>

Add my json returning from network of the console

<!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>

My original json returning, any error?

</div>
  • 写回答

1条回答 默认 最新

  • dongliao2011 2015-09-24 06:01
    关注

    On this page in the documentation it states the following:

    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

    It does not appear that your are setting these variables and this may the reason for the error message "warning:table id=example - Invalid JSON response" as your JSON, while valid JSON, is not a valid "JSON response" related to what datatables expects.

    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换