eval('('+json+')') needed when POSTing to server
eval('('+json+')') needed when POSTing to server
jborrajo
Posts: 14Questions: 0Answers: 0
Hi,
I got DataTables to work using server side processing, ASP.NET MVC with C# and a custom fnServerData function, but I had to edit the plugin code to fix a JSON deserialization issue.
"fnServerData": function(sSource, aoData, fnCallback) {
$.ajax({
"datatype": "json"
, "type": "POST"
, "url": sSource
, "data": aoData
, "success": fnCallback
});
As described in "Pro ASP.NET MVC Framework", page 450, $.getJSON deserializes a string into a JSON data object, but $.ajax does not, so it is necessary to use code like this on the JavaScript side:
json = eval('(' + json + ')');
I edited function _fnAjaxUpdateDraw like this:
if ( !json.aaData) {
json = eval('(' + json + ')');
if ( !json.aaData) {
alert("Error! invalid JSON data");
return;
}
}
HTH, regards
Javier
I got DataTables to work using server side processing, ASP.NET MVC with C# and a custom fnServerData function, but I had to edit the plugin code to fix a JSON deserialization issue.
"fnServerData": function(sSource, aoData, fnCallback) {
$.ajax({
"datatype": "json"
, "type": "POST"
, "url": sSource
, "data": aoData
, "success": fnCallback
});
As described in "Pro ASP.NET MVC Framework", page 450, $.getJSON deserializes a string into a JSON data object, but $.ajax does not, so it is necessary to use code like this on the JavaScript side:
json = eval('(' + json + ')');
I edited function _fnAjaxUpdateDraw like this:
if ( !json.aaData) {
json = eval('(' + json + ')');
if ( !json.aaData) {
alert("Error! invalid JSON data");
return;
}
}
HTH, regards
Javier
This discussion has been closed.
Replies
I don't think you needed to edit DataTables core for this actually. There is a typo in your "dataType" parameter (the case). According to the jQuery documentation this will cause the callback function to get JSON data: http://docs.jquery.com/Ajax/jQuery.ajax#options . You can see it in action here: http://datatables.net/examples/server_side/post.html
Regards,
Allan
Typo indeed!!
I corrected the typo in my code and now the plugin works fine. No need to change the core.
Thanks a lot for your support.
Javier
Can you explain me about your solution to solve this problem, please?
Thank a lot for your support
Zabahey
The probem was I used:
"datatype": "json"
which is wrong.
This is right
"dataType": "json"
Regards
Thank you very much for your Answer