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?
MarkoRocko
Posts: 1Questions: 0Answers: 0
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!
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!
This discussion has been closed.
Replies
$("#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]
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.
http://www.codeproject.com/KB/java/JQuery-DataTables-Java.aspx
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