Dirty JSON
Dirty JSON
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.
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.
This discussion has been closed.
Replies
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.
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?
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
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 :-)
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
[code] var js = eval('('+ newResponse + ')'); [/code]
How do I pass the JSON to the datatable? I think I am getting close :-)
Finally managed to clean the JSON.
Thank you so much for your wonderful support.
:-)
Nice one getting this going :-)
Allan
Sent the corrected json to fnCallback.. I did this under the "error" key, since I was initially getting an error because of incorrect JSON.