Why does DataTables only let me use a file for JSON, I want pass in a String of JSON?

Why does DataTables only let me use a file for JSON, I want pass in a String of JSON?

MarkoRockoMarkoRocko Posts: 1Questions: 0Answers: 0
edited July 2011 in General
How can I make DataTables accept a JSON String, instead of a file with JSON text?
So if I click on the download examples: C:\DataTables-1.8.1\examples\ajax
It runs nicely. However, if I just want to substitute the file location with a text String, it fails to load the data and shows "loading...".
In my case I'm trying to send down a JSON String from a Servlet dynamically. I don't want to have to create a file then point to it.
Example:
DataTables(txt file) works:
$(document).ready(function() {
var oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "sources/arrays.txt"
} );
} );

DataTables(raw text String) fails:
$(document).ready(function() {
var oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": '{ "aaData": [["Trident","Internet Explorer 4.0","Win 95+","4","X"],["Trident","Internet Explorer 5.0","Win 95+","5","C"],["Other browsers","All others","-","-","U"]] }'
} );
} );
P.S. I'm a newbie so if I'm overlooking something obvious please explain with a clear sample, thanks!

Replies

  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    edited July 2011
    [code]
    $("#Products").dataTable({
    aaData: [["a",1],["b",2]], // use aaData here, and it should be an array, rather than a string
    bJQueryUI: true, sDom: '<"H"f>t'
    ....
    });
    [/code]
  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited July 2011
    Marko,

    If I'm understanding correctly, you're receiving JSON from a Servlet. Servlets are not technically files/pages, so you don't want to have to specify a particular filename. Is that correct?

    If so, this is exactly what we are doing with our application. sAjaxSource cannot be a string because that method is specifically for accessing data via Ajax. But your Servlet can return JSON just fine as long as you program it to.

    In our application, we specify sAjaxSource as "/JSONServlet/?type=foo" (where foo equals the type of data we expect to be returned). The servlet gets the request, sees that it should return the "foo" data, and indeed sends it back as essentially a "string". We just build a string that follows the JSON pattern, and after it is built, we send it back out as the HTTP response.

    So, to recap:

    Your example won't work simply because AjaxSource means just that-- it has to be a URL that you are requesting data from. However, it does not need to be a file, it can be a URL for a Servlet.
  • StephanStephan Posts: 20Questions: 0Answers: 0
    A good example for servlet usage is found here:
    http://www.codeproject.com/KB/java/JQuery-DataTables-Java.aspx
  • KA2012KA2012 Posts: 16Questions: 0Answers: 0
    MarkoRocko: Did you find a way around this? I have the same problem as you.
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    @KA2012:

    This is JSON:

    [code]
    { "first": "1", "second": 2 }
    [/code]

    This is not (it is just a string):

    [code]
    "{ \"first\": \"1\", \"second\": 2 }"
    [/code]

    DataTables will not attempt to parse a string into JSON - it expects only a JSON object. If you want to give it a string that contains JSON representation, then you would need to use jQuery's $.parseJSON in DataTables fnServerData callback, or eval(). But I would very much suggest looking to see if you can find a way of returning JSON, not a string.

    Allan
This discussion has been closed.