eval('('+json+')') needed when POSTing to server

eval('('+json+')') needed when POSTing to server

jborrajojborrajo Posts: 14Questions: 0Answers: 0
edited September 2009 in Bug reports
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

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Javier,

    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
  • jborrajojborrajo Posts: 14Questions: 0Answers: 0
    Hi 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
  • zabaheyzabahey Posts: 5Questions: 0Answers: 0
    Hi jborrajo

    Can you explain me about your solution to solve this problem, please?

    Thank a lot for your support

    Zabahey
  • jborrajojborrajo Posts: 14Questions: 0Answers: 0
    Hi zabahey

    The probem was I used:
    "datatype": "json"
    which is wrong.
    This is right
    "dataType": "json"

    Regards
  • zabaheyzabahey Posts: 5Questions: 0Answers: 0
    Hi jborrajo

    Thank you very much for your Answer
This discussion has been closed.