Getting a parsererror when using fnServerData
Getting a parsererror when using fnServerData
brannan
Posts: 5Questions: 0Answers: 0
I'm trying to change over one of my tables from printing the whole table to a datatable. But when I try to get my data through a POST call I get a statusText of parsererror.
This is my code for the data table:
[code]
var formsTable = $j('#my_table').dataTable({
"bProcessing": true,
"bServerSide": true,
"bJQueryUI": true,
"sDom": '<"H"lfr>t<"F"ip>',
"bAutoWidth": false,
"aoColumnDefs": [],
"sAjaxSource": "../Data?m=fl",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$j.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
//"contentType": "application/json; charset=utf-8",
"success": function () {
fnCallback();
},
"error": function (xhr) {
$j('#messages').text(xhr.responseText);
}
} );
}
});
[/code]
I'm using jsf so thats why you see the $j instead of $
$j('#messages') is a plain div that shows the output of my JSON object which is as follows:
[code]
{"iTotalDisplayRecords":934,"iTotalRecords":934,"aaData":[[1,"a","a",2012-01-06 11:19:46.0,"a","a","a",2012-01-10 09:26:32.0,null,"a","a",""],[]]}
[/code]
This data works perfectly when I exclude fnServerData and the data is retrieved with a GET call to my servlet. If I uncomment the contentType line, I get the js error "json is undefined" on line 3330 of the full datatables.js file. The line is this "if ( typeof json.sEcho != 'undefined' ) " if you are looking in the code.
Anyone have any idea what format the fnServerData should be returned in? I think this is the issue.
I have tried with the JSON object being returned as [[],[]] but I still get a parsererror.
Thanks in advance!
This is my code for the data table:
[code]
var formsTable = $j('#my_table').dataTable({
"bProcessing": true,
"bServerSide": true,
"bJQueryUI": true,
"sDom": '<"H"lfr>t<"F"ip>',
"bAutoWidth": false,
"aoColumnDefs": [],
"sAjaxSource": "../Data?m=fl",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$j.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
//"contentType": "application/json; charset=utf-8",
"success": function () {
fnCallback();
},
"error": function (xhr) {
$j('#messages').text(xhr.responseText);
}
} );
}
});
[/code]
I'm using jsf so thats why you see the $j instead of $
$j('#messages') is a plain div that shows the output of my JSON object which is as follows:
[code]
{"iTotalDisplayRecords":934,"iTotalRecords":934,"aaData":[[1,"a","a",2012-01-06 11:19:46.0,"a","a","a",2012-01-10 09:26:32.0,null,"a","a",""],[]]}
[/code]
This data works perfectly when I exclude fnServerData and the data is retrieved with a GET call to my servlet. If I uncomment the contentType line, I get the js error "json is undefined" on line 3330 of the full datatables.js file. The line is this "if ( typeof json.sEcho != 'undefined' ) " if you are looking in the code.
Anyone have any idea what format the fnServerData should be returned in? I think this is the issue.
I have tried with the JSON object being returned as [[],[]] but I still get a parsererror.
Thanks in advance!
This discussion has been closed.
Replies
Allan
The debug code for my new issue is alufex.
Thanks a lot for your help!
Allan
Here is the new debug code: umeqom
If you look at the Server Interaction section you will see some code that pulls the success aaData, which is returning a list of my data.
Is there a specific format the data is supposed to be in? I'm not sure if you can see this, but x contains 3 values (aaData, iTotalDisplayRecords, iTotalRecords). Is this what is expected as return values? These are the same values I am using elsewhere when populating a table using the normal get functionality.
Thanks!
Click you user name at the top of the page in the nab bar and this pick the 'My Preferences' option in the side bar and select what you want (remember to check your spam folder just in case!).
Regarding the issue - you need to call fnCallback and give DataTables the returned object form the server with aaData, iTotalDisplayRerecords etc. Probably something like this:
[code]
success: function(x) {
var jj = x.aaData;
fnCallback( x );
},
[/code]
Allan
Everything seems to be working great, except it looks like the fnServerData section is getting called twice on page load for some reason. The table isn't being initialized anywhere else and there are no fnDraw methods being called on page load. I put a firebug break point at the first line in the function for fnServerData and that gets hit twice. A break point at the line below the .dataTable({ line only gets called once as expected. The new deug code is iqedah.
Awesome support Allan! I'll have to put some money towards your beer fund.
Great to hear you got it going!
Allan