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

时间:2022-10-07 15:25:13

I have googled for the answer of this question but noone helped. I am getting error mentioned in title.

我搜索了这个问题的答案,但没有人帮忙。我在标题中提到了错误。

Here is my ajax code :

这是我的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" }
        ]
    } )

I validated Response from server ( as seen in Developer tools) and its being shown as Valid JSON. But its not reflected in page.

我验证了来自服务器的响应(如开发人员工具中所示),并显示为有效JSON。但它没有反映在页面中。

My HTML Code is

我的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>

UPDATE

Response from Server(there are plenty of entries. I am showing one as example):

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

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

Any help will be appreciated.

任何帮助将不胜感激。

3 个解决方案

#1


2  

I struggled with this. and after I RTM here https://www.datatables.net/examples/ajax/custom_data_flat.html I figured it out.

我为此苦苦挣扎。在我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 calls are done a certain way. not just ajax. but you need to include the url and dataSrc variables in there. After I did that with my code everything worked as it should.

ajax调用是以某种方式完成的。不仅仅是ajax。但是你需要在那里包含url和dataSrc变量。在我用我的代码完成后,一切正常。

See if this works for you.

看看这是否适合你。

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

#2


0  

Think the problem is paymentMode. It is written without underscore in the table definition and with underscore in your json ("payment_mode":)

认为问题是paymentMode。它在表定义中没有下划线,在json中使用下划线(“payment_mode”:)

#3


0  

The answer should look like this:

答案应如下所示:

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

Look at here: http://json.parser.online.fr/

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

#1


2  

I struggled with this. and after I RTM here https://www.datatables.net/examples/ajax/custom_data_flat.html I figured it out.

我为此苦苦挣扎。在我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 calls are done a certain way. not just ajax. but you need to include the url and dataSrc variables in there. After I did that with my code everything worked as it should.

ajax调用是以某种方式完成的。不仅仅是ajax。但是你需要在那里包含url和dataSrc变量。在我用我的代码完成后,一切正常。

See if this works for you.

看看这是否适合你。

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

#2


0  

Think the problem is paymentMode. It is written without underscore in the table definition and with underscore in your json ("payment_mode":)

认为问题是paymentMode。它在表定义中没有下划线,在json中使用下划线(“payment_mode”:)

#3


0  

The answer should look like this:

答案应如下所示:

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

Look at here: http://json.parser.online.fr/

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