javascript - Ajax 不为数据表调用 php 页面

标签 javascript php jquery ajax datatables

我正在一起测试 php 和数据表。我首先设计了一个简单的表格作为

<table id="activeDriverGrid"  name='activeDriverGrid'  class="table table-striped table-bordered bootstrap-datatable datatable ">
                          <thead>
                              <tr>
                                  <th>Driver Name</th>
                                  <th>CNumber</th>                                
                              </tr>
                          </thead>   

                      </table> 

只是为了确认我有这个脚本

<script src='js/jquery.dataTables.min.js'></script>

最后在下面我称之为。

<script type="text/javascript" language="javascript" >
            $(document).ready(function() {
                alert("calling");
                 $('#activeDriverGrid').dataTable({
            "bDestroy": true
        }).fnDestroy();

                var dataTable = $('#activeDriverGrid').DataTable( {
                    "processing": true,
                    "serverSide": true,
                    "ajax":{
                        url :"getData.php", // json datasource
                        type: "post",  // method  , by default get
                        success: function (data) {
                 alert("sucess");
            },
                        error: function(){  // error handling
                            $(".activeDriverGrid-error").html("");
                            $("#activeDriverGrid-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
                            $("#activeDriverGrid-grid_processing").css("display","none");

                        },
                        "bDestroy": true
                    }
                } );
            } );
        </script>

这是我的 getData.php 脚本

try {

        $requestData= $_REQUEST;


        $columns = array( 
        // datatable column index  => database column name
            0 =>'driverID', 
            1 => 'driverName',
            2=> 'driverContactNumber'
        );


         $nestedData=array(); 

         $nestedData[] = "Allan";
         $nestedData[] = "2214141";

         $data[] = $nestedData;

        $totalData=1;
    $totalFiltered = $totalData;
        $json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
            "recordsTotal"    => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data"            => $data   // total data array
            );

    echo json_encode($json_data);  // send data as json format

}
catch(PDOException $pe)
{
        die("Error In Get Active Drivers :" . $pe->getMessage());
}   

我手动调用了这个页面,结果是 {"draw":0,"recordsTotal":1,"recordsFiltered":1,"data":[["Allan","2214141"]]} 因此确认其工作。我已经在 google chrome 中运行了分析工具,我在网络中没有看到正在调用 getData.php,但是弹出调用出现了。

最佳答案

试试这个

  <script type="text/javascript" language="javascript" >
            $(document).ready(function() {
                alert("calling");
                 $('#activeDriverGrid').dataTable({
            "bDestroy": true
        }).fnDestroy();

                var dataTable = $('#activeDriverGrid').DataTable( {
                    "processing": true,
                    "serverSide": true,
                    });
                     $.ajax({
                        url :"getData.php", // json datasource
                        type: "post",  // method  , by default get
                        data:{data:dataTable},
                        success: function (data) {
                 alert("sucess");
            },
                        error: function(){  // error handling
                            $(".activeDriverGrid-error").html("");
                            $("#activeDriverGrid-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
                            $("#activeDriverGrid-grid_processing").css("display","none");

                        },
                        "bDestroy": true
                    })
                } );

        </script>

关于javascript - Ajax 不为数据表调用 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35390301/

相关文章:

javascript - Reactjs:nth-​​child 的替代方案(jsx 无法正确构建 "child not defined")

javascript - $(window).load(function() 完全加载文档后无法执行

javascript - SELECT 输入仅从数据库返回有限的值

javascript - "replace is not a function"

php - 将组合框中的所选项目设置为 "please select..."

php - 原则一对多关系不会保存 - 违反完整性约束

jquery - 当数据属性更新时Knockout Js更新ViewModel

javascript - Vue js Ready 功能未触发

javascript - Jquery 中的 Ajax 自动完成无法显示结果

javascript - 无法读取未定义的属性 'ref'