sAjaxSource; Data is not displayed

sAjaxSource; Data is not displayed

beginner_beginner_ Posts: 55Questions: 2Answers: 0
edited August 2011 in General
Hi all,

I have a datatable that does not display the JSON data. I can see in Firebug that it is returned. One possible issue: is there a size limit? In my case it's about 3000 rows of text data. Not really that much.

Here my code. Note that if I set the GET parameter mixtureName, this actually works. (data is displayed).

[code]
mixtureTable = $('#mixture').dataTable( {
"sAjaxSource": "getSamplesJSON.php",
"bDeferRender": true,
"iDisplayLength": -1,
"aLengthMenu": [[5, 10, 20, -1], [5, 10, 20, "All"]],
"sScrollY": "160",
"aoColumns": [
/* Code */ { "bSearchable": false,
"bVisible": false },
/* Ename */ null,
/* Date */ {"sType": "date"},
/* Remarks */ null,
/* Source */ null
],
"fnServerData": function ( sSource, aoData, fnCallback ) {
if (urlParams.hasOwnProperty("mixtureName")){
/* Add some data to send to the source, and send as 'GET' */
aoData.push( { "name": "mixtureName", "value": urlParams["mixtureName"] } );
}
$.ajax( {
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
});
[/code]

Replies

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin
    > One possible issue: is there a size limit? In my case it's about 3000 rows of text data.

    No - no size limit :-)

    I would guess that there is a JSON encoding issue with the data. Have a look at the returned XHR source in Firebug or Inspector and see what is in there. Run the text through http://jsonlint.com as well to make sure it is valid.

    Allan
  • beginner_beginner_ Posts: 55Questions: 2Answers: 0
    edited August 2011
    EDIT:

    Found the solution.

    Some of the textual data contains "\" which causes issues.
    Solution was to escape it:
    [code]
    $remarks = str_replace('\\', '\\\\', $remarks);
    [/code]
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    good catch. I will add this to the debugging blog.
  • beginner_beginner_ Posts: 55Questions: 2Answers: 0
    FYI:
    Above problem occured when creating the json in php by string concatentation because I had trouble getting the right format that datatables requires. If you know how to make the php arrays correctly that you can use json_encode php function, replacing is not needed (plus you get a noticeable speed gain).
    htmlentities is in my case required because my string data contains double quotes.

    [code]
    $json = array("aaData" => array());
    while ($preparedStatement->fetch()) {

    $jsonRow = array();

    $jsonRow[] = htmlentities($myColumn1);
    $jsonRow[] = htmlentities(($myColumn2);
    ...
    $json['aaData'][] = $jsonRow;
    }
    $preparedStatement->close();
    $con->close();

    header('Content-type: application/json');
    echo json_encode($json);
    [/code]
This discussion has been closed.