php - 数据表警告 : table id=example - Invalid JSON response

标签 php html mysql ajax

我用谷歌搜索了这个问题的答案,但没有人提供帮助。 我收到标题中提到的错误。

这是我的 ajax 代码:

var table = $('#example').DataTable( {
        "ajax": "<?php echo $root; ?>ajax/order.php",
        "processing": true,
        "serverSide": true,
        "ordering": false,
        "searching": true,
        "columns": [
            { "data": "order_no" },
            { "data": "country" },
            { "data": "name" },
            { "data": "date","className": "align-center" },
            { "data": "subtotal","className": "align-right" },
            { "data": "dileveryAmt","className": "align-right" },
            { "data": "totalAmt","className": "align-right" },
            { "data": "paymentMode","className": "align-center" },
            { "data": "payment","className": "align-center" },
            { "data": null,"defaultContent": "<button id='view' class='btn btn-small btn-info'>View</button><button id='delete' class='btn btn-small btn-danger'>Delete</button>","className": "align-center" }
        ]
    } )

我验证了来自服务器的响应(如在开发者工具中所见)并将其显示为有效 JSON。但它没有反射(reflect)在页面中。

我的 HTML 代码是

<table id="example" class="display table-bordered" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Order No</th>
        <th>Country</th>
        <th>Customer</th>
        <th>Date</th>
        <th>Sub Total</th>
        <th>Delivery Charge</th>
        <th>Total</th>  
        <th>Payment Mode</th>
        <th>Payment</th>
        <th>Action</th>            
    </tr>
</thead>

更新

来自服务器的响应(有很多条目。我以一个为例):

"data": [
    {
        "id": "183",
        "customer_id": "183",
        "subtotal": "0.00",
        "totalAmt": "0.00",
        "dileveryAmt": "0.00",
        "date": "18/02/2015",
        "midnightdelivery": "0",
        "delivery_date": "2015-02-19",
        "message_on_cake": "",
        "special_instruction": "",
        "payment": "<div class='label label-warning'>Pending</div>",
        "delivery": "0",
        "created": "2015-02-18 10:58:29",
        "ip": "",
        "payment_mode": "",
        "first_name": "Ganesh",
        "last_name": "Salunkhe",
        "email": "g@s.com",
        "address": "",
        "flat_no": "k",
        "building_name": "k",
        "street": "k",
        "area": "k",
        "landmark": "k",
        "city": "mumbai",
        "country": "India",
        "state": "maharashtra",
        "contact_no": "7666902899",
        "name": "Ganesh Salunkhe",
        "order_no": "1181"
    },

任何帮助将不胜感激。

最佳答案

我为此苦苦挣扎。在我在这里 RTM 之后 https://www.datatables.net/examples/ajax/custom_data_flat.html我想通了。

 ajax: {
        url: "data/objects_root_array.txt", <-- notice url and the link
        dataSrc: "" <-- if you are just going to json_encode the result from the db. it will handle a flat string
    }

ajax 调用以某种方式完成。不只是 Ajax 。但您需要在其中包含 url 和 dataSrc 变量。在我用我的代码完成该操作后,一切正常。

看看这是否适合您。

var table = $('#example').DataTable( {
    "ajax": {
        url: "<?php echo $root; ?>ajax/order.php",
        dataSrc : ""
    },
    "processing": true,
    "serverSide": true,
    "ordering": false,
    "searching": true,
    "columns": [
        { "data": "order_no" },
        { "data": "country" },
        { "data": "name" },
        { "data": "date","className": "align-center" },
        { "data": "subtotal","className": "align-right" },
        { "data": "dileveryAmt","className": "align-right" },
        { "data": "totalAmt","className": "align-right" },
        { "data": "paymentMode","className": "align-center" },
        { "data": "payment","className": "align-center" },
        { "data": null,"defaultContent": "<button id='view' class='btn btn-small btn-info'>View</button><button id='delete' class='btn btn-small btn-danger'>Delete</button>","className": "align-center" }
    ]
} )

关于php - 数据表警告 : table id=example - Invalid JSON response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28600560/

相关文章:

php - 过滤 MySQL 中的项目

php - 使用 openssl_encrypt AES-CBC 实现 Python 到 PHP 兼容的 AES 加密

javascript - 根据数据库结果填充li内容

mysql - 如果没有结果,则将查询中的所有值初始化为 0,将 top 初始化为 NULL

javascript - 服务器端事件开始/停止

php - 客户希望我(仅)进行 CSS 编码,但不想向我提供 php 文件

php - 通知: Undefined index: username PHP and MySQL

javascript - IE 错误 'var' 未定义

javascript - 如何更改 Angular Material 按钮的宽度?

mysql - 如何修复 : "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools?