JSON formatting error after upgrading to DataTables 1.8 and jQuery 1.4.3

JSON formatting error after upgrading to DataTables 1.8 and jQuery 1.4.3

jimvjimv Posts: 27Questions: 0Answers: 0
edited August 2011 in DataTables 1.8
I upgraded to DataTables 1.8, at the same time as making some other changes to our website. One of which was also to upgrade jQuery from v1.3.2 to v1.4.3.

After upgrading, I get a JSON error:
[quote]
"DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.
The JSON data comes from a php file, which was not changed; the data looks the same so far as I can tell.
[/quote]

The head of the JSON data is:
[quote]
{"sEcho": , "iTotalRecords": 7475, "iTotalDisplayRecords": 7475, "aaData": [ ["xx","xx","xx","Delafield","WI","53018","School of Nursing","Dean\'s Honor List"],["xx","xx","xx","Lake Tomahawk","WI","54539","College of Letters and Science","Dean\'s List"],["xx","xx","xx","Campbellsport","WI","53010","College of Engineering","Dean\'s Honor List"],["xx","xx","xx","Carver","MN","55315","College of Agricultural and Life Sciences","Dean\'s List"],..."
[/quote]

If I switch back to jQuery 1.3.2, everying works fine.

My DataTables initialization is:
[code]
var oTable;
var asInitVals = new Array();
$(document).ready(function() {
$('#static').addClass("js_enabled");
$('#dynamic').removeClass("js_enabled");
oTable = $('#deans_list').dataTable( {
"bProcessing": true,
"bServerSide": true,
"aaSorting": [[0,'asc'], [1,'asc']],
"sPaginationType": "full_numbers",
"bAutoWidth": false,
"bSortClasses": true,
"oLanguage": {
"sSearch": "Search all columns:"
},
"aoColumns": [ {"sWidth": "90PX" },
{ "bSortable": false, "sWidth": "90PX" },
{ "bSortable": false, "sWidth": "90PX" },
{"sWidth": "100PX" },
{"sWidth": "70PX" },
{ "sType": "numeric", "sWidth": "70PX" },
{"sWidth": "245PX" },
{"sWidth": "245PX" }],
"sAjaxSource": '/apps/dg_server_processing.php'
} );

$("#sel_college").change(function()
{
//alert($(this).val());//debug
oTable.fnFilter($(this).val());
});


$("tfoot input").keyup( function () {
/* Filter on the column (the index) of this element */
oTable.fnFilter( this.value, $("tfoot input").index(this) );
} );



/*
* Support functions to provide a little bit of 'user friendlyness' to the textboxes in
* the footer
*/
$("tfoot input").each( function (i) {
asInitVals[i] = this.value;
} );

$("tfoot input").focus( function () {
if ( this.className == "search_init" )
{
this.className = "";
this.value = "";
}
} );

$("tfoot input").blur( function (i) {
if ( this.value == "" )
{
this.className = "search_init";
this.value = asInitVals[$("tfoot input").index(this)];
}
} );
} );
[/code]

For now, I am using jQuery v1.3.2, but would like to move to the most current version of jQuery.
Any suggestions appreciated.
Thanks,
Jim

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I'd suggest passing your JSON through http://jsonlint.com . Also your sEcho parameter above doesn't have a value which would cause an error.

    Allan
  • jimvjimv Posts: 27Questions: 0Answers: 0
    Allan,
    sEcho was empty because I was just hitting my PHP directly, and was not passing in a parameter for it. If I call [code]file.php?sEcho=3[/code] then sEcho has a value as expected.

    Thanks for the suggestion of using jsonlint, that identified the problem with my json data.
    In my json data, I have [quote]"Dean\'s Honor List"][/quote], which is not valid JSON. The \ before the ' is causing the error.

    The \ is being inserted because in my PHP file, I have addslashes. In my database, I have [quote]Dean's[/quote]. So addslashes adds the \, which makes the JSON data invalid.

    I've updated my PHP file to use the latest version, that uses json_encode instead of addslashes and that fixed the issue.
    Thanks
    Jim
This discussion has been closed.