DataTables警告:table id = example - 无效的JSON响应

时间:2015-02-19 07:14:16

标签: 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。但它没有反映在页面中。

我的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"
    },

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

我为此苦苦挣扎。在我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" }
    ]
} )

答案 1 :(得分:0)

认为问题是paymentMode。它在表定义中没有下划线,在json("payment_mode":

中使用下划线

答案 2 :(得分:0)

答案应如下所示:

{
    "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"
    }
] 
}

请看这里:http://json.parser.online.fr/