DataTables warning: JSON data from server could not be parsed.

DataTables warning: JSON data from server could not be parsed.

bradley32bradley32 Posts: 16Questions: 0Answers: 0
edited February 2011 in General
For some reason, this just started happening. I am getting this error when trying to load in my JSON data with AJAX:
"DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error."

Firebug is giving this error:
Invalid label
{"aaData:" [

Here is the HTML, JS and JSON. I have no idea what happened or why...does anyone here?

Table HTML:
[code]



One
Two
Three
Four
Five
Six
Seven






[/code]
Javascript:
[code]
$('#test_table').dataTable({
"sAjaxSource" : 'dt_ajax.txt'
});
[/code]

JSON Data:
[code]
{"aaData": [
[
"",
"3S",
"38",
"09",
"300000",
"500000",
"Test data"
],
[
"",
"5I",
"34",
"12",
"500000",
"1000000",
"aaaaa"
],
[
"",
"Jumbo",
"9",
"8",
"888888",
"8888888",
"testtt"
]
]}
[/code]

Thanks in advance! P.S. The data works fine when input directly. Only "sAjaxSource" is getting this error.
«13

Replies

  • nicochapnicochap Posts: 2Questions: 0Answers: 0
    I have exactly the same problem.
    On my side it is related to the Jquery version change.
    With Jquery 1.4.4 it works fine, but in 1.5 i have the above said error
  • GregPGregP Posts: 487Questions: 8Answers: 0
    edited February 2011
    I have JSON parse errors from time to time as well; since my application is polling every few seconds, I just let my application ignore the errors (modified it so that an alert box does not appear). I am also using jQuery 1.5. I did not test extensively in 1.4.x before switching to 1.5 because I started using DataTables close to the same time that 1.5 came out.

    Interestingly, though, I only rarely get the parse error. Code is almost entirely logical, meaning that in a controlled environment, results should either consistently fail or consistently succeed. I'm not sure why it only *periodically* fails to parse properly for me, but I have to admit I have always simply believed that it was the factor OUT of my control (the communication channel between client and server) that was causing the failure, rather than jQuery or DataTables.

    Now that I'm thinking about it more carefully, though, it's worth mentioning that my parse errors almost always happen near initial page load.

    Since JavaScript executes asynchronously, I wonder if in some scenarios there is a timing issue. I am retrieving my JSON with AJAX and server-side processing, and perhaps it's usually taking Juuuuust long enough to get the data; whereas reading it in with a simple HTTP request in the initialize object (dt_ajax.txt), the data arrives before other relevant functions have terminated...? Subsequent refreshes don't need to build the entire DOM from scratch, so the timing of activity for fetching data and rendering the page will be different; something about that difference makes it less likely that I see a parse error once I'm past the initial page load.

    It's just a guess out of the blue, I do wonder if there's a timing issue somewhere.

    Greg
  • GregPGregP Posts: 487Questions: 8Answers: 0
    Did some quick testing; dropped in 1.4.4, and refreshed my page several times (to force initial page load which is where I usually see the error).

    About one in ten times, I still see the error. Swapped back in 1.5, and same results.. the error didn't seem to appear (for me at least, with my particular environment) any more or less frequently.

    I'm not 100% sure what conclusion to draw. You don't want JSON parse errors EVER, but as I said before I just let my application ignore them when they do appear. But I don't see 1.5 being worse than 1.4.4 in my scenario. They BOTH actually produce a parse error from time to time. It could still be timing... in 1.5, they no doubt refactored certain functions (and I know they overhauled the AJAX module) which might make the error appear more consistently in your particular scenarios.
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Another thread discussing this: http://datatables.net/forums/comments.php?DiscussionID=3993&page=1#Item_16 . Are you using the validation plug-in for jQuery - that appears to cause the issue you are seeing with jQuery 1.5.

    Allan
  • bradley32bradley32 Posts: 16Questions: 0Answers: 0
    Yes indeed it was the Validation plug-in. Thank you all for your help!
  • RockbRockb Posts: 97Questions: 0Answers: 0
    Okay, it might be the validation plugin-in, but HOW to deactivate or in general: How to solve this big issue?
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Two ways: 1. remove the validation plug-in, 2. upgrade to the very latest validation plug-in code ( https://github.com/bigfix/jquery-validation/commit/9aa53f9241224ba349f9fe1b2c801be0f431b63b ). There are quite a few discussions on the web about this issue with jQuery 1.5 and the jQuery validation plug-in ( http://www.google.com/#q=jquery+1.5+validate ). Basically it appears that the plug-in wraps and overwrites the $.ajax stuff, thus causing the problem since Ajax for an overhaul in jQuery 1.5.

    Allan
  • medSwamedSwa Posts: 22Questions: 0Answers: 0
    hello i have the same problem...but iam not able to find out where or how i can disable the validate plug in. i don kno if i hav one. where should i look to find it out. i use jquery.dataTables.min 1.7.6 and jquery.js is 1.2.6. and i am testing on firefox. i print the response in my log4j and the format looks correct.please help.
    my response looks like this:

    { "sEcho": 1, "iTotalRecords": 10, "iTotalDisplayRecords": 10, "aaData": [ ["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"],["1","medata","001","1","tom"] ] }

    -medSwa
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    DataTables required jQuery 1.3 or newer due to some of the calls made. If you can upgrade that, I would do so.

    Allan
  • ferytinoferytino Posts: 2Questions: 0Answers: 0
    edited February 2012
    Hi Allan,

    I've the same issue above. I just doing some test and use your jquery & datatables from your server
    http://datatables.net/release-datatables/media/js/jquery.js - jQuery v1.7.1
    http://datatables.net/release-datatables/media/js/jquery.dataTables.js - 1.9.0

    and below are my json data format and validated already through www.JSONLint.com
    [code]
    {
    "sEcho": 0,
    "iTotalRecords": "34635",
    "iTotalDisplayRecords": "34635",
    "aaData": [
    [
    "MT",
    "RTWWA",
    "Kita liat aja nanti asjjdasjjasjfjasfjjajasjfjasjf",
    "Kita liat aja nanti asjjdasjjasjfjasfjjajasjfjasjf",
    "Kita liat aja nanti asjjdasjjasjfjasfjjajasjfjasjf"
    ],
    [
    "wrwr",
    "dsfdsg",
    "ewrewr",
    "ewrewr",
    "ewrewr"
    ],
    [
    "wrwer",
    "ewrew",
    "ewrew",
    "ewrew",
    "ewrew"
    ]
    ]
    }
    [/code]

    but I got Browser Error Message "DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error".

    could you advise me what's the problem of this issue above?

    Thanks in advance.
  • ferytinoferytino Posts: 2Questions: 0Answers: 0
    Hi Allan,

    Just ignore my question above. the problem solved, already. Thanks!
  • Such_SureshSuch_Suresh Posts: 1Questions: 0Answers: 0
    Hello Allan,

    "DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error".
    could give me the solution ? my json data format from server. but my data comes in body. i could not display the data in table. could you help me?
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    > could give me the solution ?

    Absolutely - use correctly formatted JSON ;-).

    In all seriousness, use http://jsonlint.com to find out why your JSON isn't valid and firebug to get the JSON reply from the server.

    Allan
  • mgarciamgarcia Posts: 1Questions: 0Answers: 0
    Hi Allan,
    congratulations for your excellent job.

    I am using
    jquery-1.7.1
    jquery.dataTables 1.9.0

    I have validated successful my JSON data using http://jsonlint.com
    and the same JSON data works fine on other jQuery / xhr calls.

    Do you see any other reasons about this issue?

    Thanks.

    Martin


    {
    "rows": [
    {
    "row": {
    "ID": "183",
    "Field1": "Barrett Rollins",
    "Field2": "1-759-171-2284",
    "Field3": "auctor.vitae@Aeneanegestashendrerit.ca",
    "Field4": "311 Egestas Rd.",
    "Field5": "Vancouver",
    "Field6": "M3R 2HN",
    "Field7": "Taiwan, Province of China",
    "Field8": "nisi. Mauris nulla. Integer urna. Vivamus molestie dapibus ligula. Aliquam",
    "Field9": "Lorem ipsum dolor sit",
    "Field10": ""
    }
    },
    {
    "row": {
    "ID": "184",
    "Field1": "Lee Terrell",
    "Field2": "1-366-499-3352",
    "Field3": "netus@Cras.org",
    "Field4": "Ap #498-1999 Consectetuer Rd.",
    "Field5": "Brookings",
    "Field6": "30324",
    "Field7": "Papua New Guinea",
    "Field8": "mauris a nunc. In at pede. Cras vulputate velit eu",
    "Field9": "Lorem ipsum dolor",
    "Field10": ""
    }
    }
    ]
    }
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Are you using both mDataProp and sAjaxDataProp to tell DataTables to look at "rows" and the object parameters in your JSON?

    Allan
  • mouhamouha Posts: 1Questions: 0Answers: 0
    Hi, I tested my JSON and it's valide but i have this error when i try to use datatbles to show my data: "DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error".

    Any help please????
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Please run your table throughout he debugger and link me to the result.

    Allan
  • dtlewisdtlewis Posts: 7Questions: 0Answers: 0
    I consistently get this error when logging out of my website and leaving a page that has an automatically updating DataTable. I suspect it has something to do with leaving the page during a data poll.
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Very probably yes - are you using Firefox, I've notice this happening there before. What you could do is set DataTables to exception rather than 'alert' error mode:

    [code]
    $.fn.dataTableExt.sErrMode = 'throw';
    [/code]

    Allan
  • DUCKFACEDUCKFACE Posts: 24Questions: 0Answers: 0
    a have the samoe error: DataTables warning: JSON data from server could not be parsed
    php returns this json
    {

    "sEcho":0,
    "iTotalRecords":"933",
    "iTotalDisplayRecords":"933",
    "aaData":[
    [
    {
    "firm_name":"ЕЛЕКТРОСНАБДЯВАНЕ",
    "date_ot":"15.09.1979",
    "altnameto":"01.12.1987",
    "kodnkpd":"74122009",
    "dlujnost":"Електромонтьор"
    },
    {
    "firm_name":"ТКСС ТЕЛЕКОМПЛЕКТ",
    "date_ot":"25.02.1988",
    "altnameto":"09.10.1989",
    "kodnkpd":"31153019",
    "dlujnost":"Техник механик съобщителни и свързочни съоръжения"
    },
    {
    "firm_name":"ЕЛЕКТРОРАЗПРЕДЕЛЕНИЕ ВАРНА АД",
    "date_ot":"19.10.1989",
    "altnameto":"29.12.2005",
    "kodnkpd":"74122009",
    "dlujnost":"Електромонтьор"
    },
    {
    "firm_name":"ЕНЧЕВ ТРАНС ЕООД",
    "date_ot":"11.10.2006",
    "altnameto":"05.12.2007",
    "kodnkpd":"52430001",
    "dlujnost":"Пласьор стоки"
    }
    ]
    ]

    }

    and the JS


    $(document).ready(function() { $('#table_rabotni_mesta').dataTable( {"bProcessing": true, "bServerSide": true, "sAjaxSource": "table_server.php", "sServerMethod": "POST",
    //ColReorder
    "sDom": 'T<"clear">Rlfrtip',
    "bJQueryUI": true, "sPaginationType": "full_numbers",

    //translation plugin
    "oLanguage": { "sUrl": "../lang/dataTables.bg.txt"},

    "fnServerParams": function ( aoData ) {
    aoData.push( { "name": "table", "value": "" } );
    aoData.push( { "name": "blah", "value": "blahblah" } );
    aoData.push( { "name": "thing", "value": "thingsvalue" } );
    } } ); } );


    here is the HTML:
    Зареждане на информацията от сървъра

    WHERE DID I GET WRONG???


    WHERE DD I GET WRONG
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Please link us to the page.
  • DUCKFACEDUCKFACE Posts: 24Questions: 0Answers: 0
    http://expert-2002.com/multisys/support/table_view.php
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    And right enough the server is not returning JSON. It is returning:

    [code]
    ORDER BY p.firm_name as firm_name asc<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">Error on expert-2002.combody { color: #444444; background-color: #EEEEEE; }h1 { font-size: 2em; }h2 { font-size: 1.2em; }h3 { font-size: 1em; }#page{ background-color: #FFFFFF; width: 60%; margin: 24px auto; padding: 12px; }#header { padding: 6px ; text-align: center; background-color: #475076; color: #FFFFFF; }#content { padding: 4px 0 24px 0; }#footer { color: #666666; background: #f9f9f9; padding: 10px 20px; border-top: 5px #efefef solid; font-size: 0.8em; text-align: center; }#footer a { color: #999999; }FOUND ERROR !!!Bad query extentions: SELECT SQL_CALC_FOUND_ROWS p.firm_name as firm_name, DATE_FORMAT(p.date_ot, \'%d.%m.%Y\' )as date_ot, DATE_FORMAT(p.date_do, \'%d.%m.%Y\' )as altnameto, nkpd.kod_2012 as kodnkpd, nkpd.dlujnost as dlujnost FROM personal_staj p LEFT JOIN db_nkpd nkpd ON nkpd.id=p.id_nkpd ORDER BY p.firm_name as firm_name asc LIMIT 0, 10More information about this may be available in the expert-2002.com server error log.©Copyright 2002-2012 eXpert-2002.com | дизайн и хостинг еКсперт-2002
    [/code]

    Allan
  • DUCKFACEDUCKFACE Posts: 24Questions: 0Answers: 0
    are you shure .. because i se the json format without those
    server file is http://expert-2002.com/multisys/support/table_server.php
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    100% certain. Use Firebug and you'll see what the server is responding with.

    Allan
  • DUCKFACEDUCKFACE Posts: 24Questions: 0Answers: 0
    i got it. bad query in server side because im linking tables automaticly ... now i have [object Object] [object Object] [object Object] [object Object] [object Object] ... probably this one will be interesting for you
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    Your aaData is an array of arrays of objects. You want to remove the inner array and just have an array of objects.
  • DUCKFACEDUCKFACE Posts: 24Questions: 0Answers: 0
    edited October 2012
    okey .. i removed the inner array
    now the error is "DataTables warning (table id = 'table_rabotni_mesta'): Requested unknown parameter '0' from the data source for row 0".
    am i a total mess :D
  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    You need to tell DataTables which object property should be used for each column: http://datatables.net/blog/Extended_data_source_options_with_DataTables

    Allan
  • DUCKFACEDUCKFACE Posts: 24Questions: 0Answers: 0
    thanks a lot ... :) its WORKING :D
    now im trying to find the topic for "sorting date in DD.MM.YYYY format
    any idea allan ? :)
This discussion has been closed.