Getting a parsererror when using fnServerData

Getting a parsererror when using fnServerData

brannanbrannan Posts: 5Questions: 0Answers: 0
edited March 2012 in Bug reports
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!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    If you are getting a parse error then its because the JSON isn't valid :-). I'd suggest you pass the returned text through http://jsonlint.com to see what that says. If that doesn't help, please use the DataTables debugger ( http://debug.datatables.net ) and post the 6 letter debug code.

    Allan
  • brannanbrannan Posts: 5Questions: 0Answers: 0
    Thanks for such a quick response. You were right about the JSON not being well formed. I corrected this and there are no more errors now, but the Processing message shows up constantly and no rows are displayed. The ajax code hits the success block so thats a good sign, but I can't figure out why the data doesn't show up still.
    The debug code for my new issue is alufex.

    Thanks a lot for your help!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Thanks for providing the debug code :-). Unfortunately, because you are using an old version of DataTables, the information about what was returned from the server is not available in the debug trace (that was something added in 1.8+). Could you possibly either upgrade DataTables and re-run the debugger, or past in the JSON response from the server here?

    Allan
  • brannanbrannan Posts: 5Questions: 0Answers: 0
    Is there a way that i can set it up so i get an email when there is a response to my thread? I don't mean to have such a long gap in my response times.
    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!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    > Is there a way that i can set it up so i get an email when there is a response to my thread?

    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
  • brannanbrannan Posts: 5Questions: 0Answers: 0
    Wow I feel like an idiot for missing that in the examples.
    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.
  • brannanbrannan Posts: 5Questions: 0Answers: 0
    Nevermind about that last post, I got it straight. Another dev put a fnFilter call in the jsp page instead of the .js file.
    Awesome support Allan! I'll have to put some money towards your beer fund.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    edited March 2012
    Thanks very much (*hic*) ;-)

    Great to hear you got it going!

    Allan
This discussion has been closed.