DataTables warning: table id=example - Invalid JSON response

20,942

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

 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.

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" }
    ]
} )
Share:
20,942
Ganesh Salunkhe
Author by

Ganesh Salunkhe

Updated on May 06, 2020

Comments

  • Ganesh Salunkhe
    Ganesh Salunkhe almost 4 years

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

    Here is my ajax code :

    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.

    My HTML Code is

    <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": "[email protected]",
            "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.