Loading a table from an Ajax call

Loading a table from an Ajax call

dwschulzedwschulze Posts: 8Questions: 0Answers: 0
edited January 2014 in General
The docs all show Ajax calls loading data from a file. I'm making a real AJAX call and what happens is that an empty table gets rendered before the server has returned the data. My code is below.

The good news is that the Ajax call is being made. I can see the response on the server side. The server returns valid JSON (checked with JSON lint).

I'd appreciate any feedback on my code and pointers to examples of using Datatables with real AJAX calls (not reading JSON from files).

Thanks.

[code]








Serial Number
Sales Order
Part Number
Part Description
Shipped Date
Date Sold
Status










$(function() {
$("#example").dataTable({
"bServerSide": true,
"sAjaxSource": "http://localhost:8080/mtprest/Product/productByStatus?status=awaiting",
"aoColumns": [{
"mData":"serialNumber"
},{
"mData": "sONumber"
},{
"mData": "partNumber"
},{
"mData": "desc"
},{
"mData":"shippedDate"
},{
"mData":"soldDate"
},{
"mData":"status"
}
]
});
});





[/code]

Replies

  • dwschulzedwschulze Posts: 8Questions: 0Answers: 0
    Forgot to add what the JSON being returned by the server looks like:

    [code]
    {"iTotalRecords":12,"iTotalDisplayRecords":0,"sEcho":"","aaData":[{"productId":47209009,"serialNumber":"0909090","sONumber":"dev35001484_","partNumber":"987654KP-GL","desc":"TEST MEC","shippedDate":null,"soldDate":null,"status":"Awaiting Validation"}, ...}
    [/code]
  • dwschulzedwschulze Posts: 8Questions: 0Answers: 0
    I put the JSON output from my web service into a file and changed my code to

    [code]
    "sAjaxSource": "product.awaiting.json",
    [/code]

    and it worked. So it is a problem with Datatables not waiting for the web service call to return with the data.
  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    > "sEcho":""

    sEcho should be 1 or higher - http://datatables.net/usage/server-side . Might possibly be that...

    Allan
  • dwschulzedwschulze Posts: 8Questions: 0Answers: 0
    The link you provided says that the server returns what the client side sent:

    [quote]An unaltered copy of sEcho sent from the client side. [/quote]

    My AJAX call is a GET to a restful web service and I include one parameter. Do I also have to send sEcho, and if so where do I get the current value of sEcho?

    Is sEcho an int that I increment with each call starting at 1?
This discussion has been closed.