Dirty JSON

Dirty JSON

phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
edited July 2010 in General
Hello Everyone,

I ran into a issue where the server is appending some extraneous information along with my JSON data. I have no idea why.
This is the response that I am getting from the server..
[code]
{"sEcho": 1, "iTotalRecords": 1, "iTotalDisplayRecords": 1, "aaData": [ ["1","Hello","2","Danny","XYZ","Sending to XYZ"]] }<!-- hostname compressed/chunked Wed Jul 28 21:46:26 PDT 2010 -->
[/code]

As you can see the extra information, "<!-- hostname compressed/chunked Wed Jul 28 21:46:26 PDT 2010 -->"
is causing the JSON formatting error.

Is there a place where I can remove the extra information? Any help would be highly appreciated.

Replies

  • allanallan Posts: 63,405Questions: 1Answers: 10,452 Site admin
    You can use fnServerData( http://datatables.net/usage/server-side#fnServerData ) to create the XHR to get the data yourself, and then modify it on return. You'll need to get the text, rather than having jQuery do the direct to JSON conversion for you.

    Allan
  • phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
    Hi Allan,

    Thank you so much for your response.
    I changed my code to use funServerData
    [code]
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) {
    fnCallback(json)
    } );

    }
    [/code]

    How do I build the JSON or how do I edit the aoData to remove the extra information in my JSON.
    Can you please provide me a small example?

    Many Thanks.
  • allanallan Posts: 63,405Questions: 1Answers: 10,452 Site admin
    $.getJSON won't work in this case since your server isn't returning valid JSON data. If you can remove the HTML comment from your server return - I'd do that, since it will probably be easier. Otherwise you'll need to use $.ajax() to build the request, get the text return string, remove the comment, parse into json and then pass to DataTables.

    Allan
  • phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
    Hi Allan,

    When you say remove the HTML comment, are you talking about the extra parameter that is sent by the server?

    Also, I have the json object before I make the fnCallBack.. can I edit that instead?
  • allanallan Posts: 63,405Questions: 1Answers: 10,452 Site admin
    This part makes the JSON not valid: "<!-- hostname compressed/chunked Wed Jul 28 21:46:26 PDT 2010 -->". If that can be removed (i.e. not sent by the server - that would be good and make things easier on the client-side). That line will cause getJSON and anything that tries to parse the string as JSON to fail.

    So before you parse the string as JSON, you need to remove that string. Then you can procced with being with the JSON as needed.

    > Also, I have the json object before I make the fnCallBack.. can I edit that instead?

    Yes you can - but I don't believe your code is getting that far due to the invalid JSON.

    Allan
  • phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
    Hmm,

    You are right, my code isn't reaching inside the fnCallBack(json), so I cannot edit the json object.

    I have no idea why the server is appending that information to my JSON, hence I have to clear it out manually.

    Now, the biggest question is... how do I do it :-), where is the return string located? Any example would be so much helpful :-)
  • allanallan Posts: 63,405Questions: 1Answers: 10,452 Site admin
    http://api.jquery.com/jQuery.ajax/ - you want the responseText property of the XHR which is returned from the server. Do a String.replace() on it to remove your HTML comment, and then eval() the JSON string to get the JSON object :-)

    Allan
  • phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
    Hi Allan,

    I did some brain storing as I am a absolute newbie to javascript and ajax :D
    I was finally able to correct my response string, but I am not sure how and where to use the eval for JSON.
    Also, please note that I had to set aync to false, else the response string was coming as blank.

    [code]
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    var xhr = $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "async":false,
    "success": fnCallback
    });
    alert("Response: "+ xhr.responseText);
    var lastIndex = new Number(xhr.responseText.lastIndexOf("<"));
    alert("lastIndex: "+lastIndex);
    var newResponse = xhr.responseText.substring(0,lastIndex);
    alert("New Response: "+ newResponse);



    },

    [/code]
    I would be very thankful if you can explain how to use the eval() and send the corrected json to the datatables :-)

    Thanks
  • phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
    I got the json object using this

    [code] var js = eval('('+ newResponse + ')'); [/code]

    How do I pass the JSON to the datatable? I think I am getting close :-)
  • phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
    Hi Allan,

    Finally managed to clean the JSON.

    Thank you so much for your wonderful support.

    :-)
  • allanallan Posts: 63,405Questions: 1Answers: 10,452 Site admin
    edited July 2010
    Did you end up just passing your "js" variable to the callback function?

    Nice one getting this going :-)

    Allan
  • phpnewbiephpnewbie Posts: 14Questions: 0Answers: 0
    yep, that's right..
    Sent the corrected json to fnCallback.. I did this under the "error" key, since I was initially getting an error because of incorrect JSON.
This discussion has been closed.