Datatables Server Side processing not processing JSON output

Datatables Server Side processing not processing JSON output

arbetarbet Posts: 2Questions: 0Answers: 0
edited October 2011 in DataTables 1.8
I am implementing Datatables with server-side processing, and I am outputting correctly formatted JSON, but for some reason datatables is not loading my data. It is stuck on the "Processing..." message. Below is my JSON string:

[quote]{
"sEcho": 0,
"iTotalRecords": 1,
"iTotalDisplayRecords": 1,
"aaData": [
[
"2011-10-27 14:32:39",
"Company added: ",
"sam",
"127.0.0.1"
]
],
"sColumns": "date,description,user,ipaddr"
}[/quote]

Is this a valid JSON? If it is, what could be the other reasons for being stuck at the "Processing..." message?

Thanks in advance for your help!

Replies

  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited October 2011
    It looks right to me. I always have aaData as my last member of the array, but that shouldn't make any difference at all because data.sColumns will be found no matter the order. I also use mDataProp and a 3D array rather than the 2D model you're using. (sColumns becomes unecessary)

    It's possible that it's not the JSON but rather another syntax error. Can you open Firebug (or other Development Tool) to the console view, and see if there are any JS errors being reported?
  • arbetarbet Posts: 2Questions: 0Answers: 0
    I think the issue is with the JSON data I am sending from the server to the client. Clearly, something is missing. I even tried sending a totally unrelated JSON I got from somewhere else, and datatables did the same thing: It displayed "Processing..." but never processed anything.

    Could anyone point out any fields I might be missing?
  • HeavyChevyHeavyChevy Posts: 7Questions: 0Answers: 0
    I had a very similar problem. When I ran the script under Firebug, I observed a JSON error, even though my JSON output passed the JSONLint test. After some minor hair-pulling I noticed my includes were pulling in an older version of JSON. I removed the include and all worked perfectly. Just a thought, maybe it will help.
  • cprotocproto Posts: 3Questions: 0Answers: 0
    I seem to having a similar problem as well. One thing you might want to try is to turn server-side processing off and test it.

    My problem is that the datatable will not render the json response for server-side processing, but the EXACT same json will render correctly with server-side processing off.

    I don't have a solution yet. I have made sure to have the most up to date json, jquery, and dataTables. Problem still exists.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Server-side processing requires the server to respond with more parameters that client-side processing with Ajax sourced data: http://datatables.net/usage/server-side .

    What does your JSON return look like?

    Allan
  • cprotocproto Posts: 3Questions: 0Answers: 0
    edited November 2011
    Here's my JSON response. Everything looks right to me except that I am missing the optional sColumns param.

    [code]
    {
    "sEcho": 0,
    "iTotalRecords": 2,
    "iTotalDisplayRecords": 2,
    "aaData": [
    [
    "",
    "551110",
    "07/25/2011",
    "TPA1",
    "STD",
    4,
    "James Fulbright",
    "",
    "Attach"
    ],
    [
    "",
    "12548",
    "07/19/2011",
    "TPA1",
    "STD",
    16,
    "Wendal Lolley",
    "",
    "Attach"
    ]
    ]
    }

    [/code]

    Also here is how I initialize the table:

    HTML:
    [code]




    Claim #
    Date Received
    Source
    Claim Type
    Employee #
    Employee Name






    [/code]

    JS:
    [code]
    $(document).ready(function() {

    ...

    // claims search datatable
    var claims_search_dt = $(".claims-search-table").dataTable({
    "bAutoWidth": false,
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": '/claims/search-claims/<?php echo $this->incident->id; ?>/',
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    aoData = { has_select: 1 };
    if ($('.claims-search-table').data("searchQuery")) {
    $.extend(aoData, $('.claims-search-table').data("searchQuery"));
    }
    $.getJSON( sSource, aoData, function (json) { fnCallback(json); } );
    },
    "aoColumns": [
    { "bSortable": false },
    { "sName": "incident_number"},
    { "sName": "case_number"},
    { "sName": "employee_number"},
    { "sName": "employee_last_name"},
    { "sName": "employee_first_name"},
    { "sName": "open_date"},
    { "bSortable" : false},
    { "bSortable" : false}
    ],
    "oLanguage": {
    "sLengthMenu": "Show _MENU_",
    "sProcessing": ""
    },
    "sDom": '<"fr"l><"table-top"r>t<"table-bottom"ip>'
    });

    [/code]

    When bServerSide is set to true, the ajax request is made and returns the JSON above, but hangs on processing instead of rendering the table. When bServerSide is set to false, ajax request is made and returns the JSON above, but renders the table as expected.

    [quote]allan said: Server-side processing requires the server to respond with more parameters that client-side processing with Ajax sourced data: http://datatables.net/usage/server-side .



    What does your JSON return look like?
    [/quote]
  • cprotocproto Posts: 3Questions: 0Answers: 0
    Thanks for the response / help, allan.

    I realized that aoData was being overwritten, not extended as I intended, on Line 12 in my previous comment. None of the correct data was being sent to the server and the json response was wrong.

    This may be related to the original author's problem. I noticed that the correct value for sEcho should be 1 not 0.

    I was getting 0 because I was casting null to int 0 and it was null because the aoData sent to the server was not correct. The OP's JSON up above has an sEcho of 0 which I suspect is for similar reasons that mine was returning 0 as well.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Yup - sEcho should never be 0 :-). It should be 1 initially and then increasing once for every request to the server. This is to stop rapid Ajax requests from getting muddled up.

    So with that fixed, does it now work okay for you? The rest of your code looks fine.

    Allan
  • agucortesagucortes Posts: 2Questions: 0Answers: 0
    i'm having the same problem, my json response is well formated. I'm getting this from fire-bug.
    I don't know why the client side do not load the data.
    here is the json response
    [code]
    {
    "sEcho": 1,
    "iTotalRecords": 2,
    "iTotalDisplayRecords": 2,
    "aaData": [
    [
    "10",
    "Agustin",
    "Cortes",
    "cargo"
    ],
    [
    "14",
    "aaa",
    "bbbb",
    "Concejal"
    ]
    ]
    }
    [/code]

    here is the declaration of the table
    [code]
    $('#Tabla').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "includes/php/tablaPersonas.php",
    "iDeferLoading":2,
    "aoColumns": [{ "bVisible": false},null,null,null]
    }).makeEditable({
    sUpdateURL: "includes/php/actualizarPersona.php",
    "aoColumns": [{
    onblur: 'submit'
    },
    {
    onblur: 'submit'
    },

    {
    tooltip: 'Cargo',
    type: 'select',
    onblur: 'submit',
    data: "{<?php foreach ($cargos as $cargo){ ?>'<?php print($cargo->idCargo);?>':'<?php print($cargo->cargo);?>',<?php }?>}",
    sUpdateURL: "includes/php/actualizarPersona.php"
    }
    ],
    sAddURL: "includes/php/agregarPersona.php",
    sAddHttpMethod: "POST",
    sDeleteURL: "includes/php/quitarPersona.php",
    sDeleteHttpMethod: "POST",
    });
    [/code]
    i don't know what could be the problem... any idea?
  • agucortesagucortes Posts: 2Questions: 0Answers: 0
    here is the debug
    http://debug.datatables.net/alovic
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Can you give us a link to the page please? The debugger doesn't show a huge amount of useful information since you are using an old version of DataTables and the debugger isn't tuned for that.

    Allan
  • sadeghhpsadeghhp Posts: 3Questions: 0Answers: 0
    aaData: [{id:18, Name:Emkay Entertainments, Address:Nobel House, Regent Centre, Town:Lothian},…]
    0: {id:18, Name:Emkay Entertainments, Address:Nobel House, Regent Centre, Town:Lothian}
    Address: "Nobel House, Regent Centre"
    Name: "Emkay Entertainments"
    Town: "Lothian"
    id: 18
    1: {id:19, Name:The Empire, Address:Milton Keynes Leisure Plaza, Town:Buckinghamshire}
    2: {id:20, Name:Asadul Ltd, Address:Hophouse, Town:Essex}
    3: {id:22, Name:Ashley Mark Publishing Company, Address:1-2 Vance Court, Town:Tyne & Wear}
    4: {id:23, Name:MuchMoreMusic Studios, Address:Unit 29, Town:London}
    5: {id:24, Name:Victoria Music Ltd, Address:Unit 215, Town:London}
    6: {id:25, Name:Abacus Agent, Address:Regent Street, Town:London}
    7: {id:27, Name:Pyramid Posters, Address:The Works, Town:Leicester}
    8: {id:28, Name:Kingston Smith Financial Services Ltd, Address:105 St Peter's Street, Town:Herts}
    9: {id:29, Name:Garrett Axford PR, Address:Harbour House, Town:West Sussex}
    iTotalDisplayRecords: 93
    iTotalRecords: 100
    sEcho: "9"
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    I think you might need to give us a link to the page. That isn't valid JSON, but it might just be the formatting for Firebug or whatever it is that you are using.

    Allan
This discussion has been closed.