_fnAjaxUpdateDraw crashes on "var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );"

_fnAjaxUpdateDraw crashes on "var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );"

yasvintusyasvintus Posts: 7Questions: 0Answers: 0
edited July 2013 in DataTables 1.9
Hello,

I am sure this is something obvious, but I can't figure out what's going on. I am using a server side ajax datasource. If I have the server side method that returns some jsonp.

The datatable is set up like this (per http://datatables.net/release-datatables/examples/server_side/jsonp.html)

[code]
oTable = $('#example').dataTable({
"bServerSide": true,
"sAjaxSource": url,
"fnServerData": function (sUrl, aoData, fnCallback) {
$.ajax({
"url": sUrl,
"data": aoData,
"success": fnCallback,
"dataType": "json",
"cache": false
});
}
[/code]

When I load the page, the datatables scrip crashes because the following line results in an undefined aData object

[code]
var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );
[/code]

(line 2037 of jquery.dataTables.js v. 1.9.4)

When the crash happens the value of the json parameter in the debugger is:
{"sEcho":1,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[["Vasya","Doer","1","1/1/0001"],["Petya","Boss","2","1/1/0001"]]}

Seems valid, right?

What am I doing wrong?

Thank you!
Daniel

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Looks okay to me - please link to a test case showing the error: http://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read

    Allan
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    http://datatables.net/release-datatables/examples/server_side/scripts/jsonp.php?callback=blurg shows the jsonp is wrapped in a function name and parentheses.

    make sure you're returning valid JSONP
  • yasvintusyasvintus Posts: 7Questions: 0Answers: 0
    edited July 2013
    Thank you for your responses, guys.

    Allan -- you can see the page here: http://grq.freink.net/wcf/default.html Sorry, I should've referenced this link in the first place.

    Fbas -- yes, the jsonp response looks valid in Fiddler (see below), but once it gets into the js function where the crash happens it loses the parentheses.

    jQuery18202853988258048709_1373588742548("{\"sEcho\":1,\"iTotalRecords\":2,\"iTotalDisplayRecords\":2,\"aaData\":[[\"Vasya\",\"Doer\",\"1\",\"1\/1\/0001\"],[\"Petya\",\"Boss\",\"2\",\"1\/1\/0001\"]]}");
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Ah - took me a minute there, but your callback is giving a string, not JSON.

    i.e. currently there is:

    [code]
    _("{\"sEcho\":1,\"iTotalRecords\":2, ...
    [/code]

    You want to return JSON like:

    [code]
    _({"sEcho":1,"iTotalRecords":2, ...
    [/code]

    So a little update to your script is needed to do that.

    Allan
  • yasvintusyasvintus Posts: 7Questions: 0Answers: 0
    Thank you!

    You mean the server-side script, right? I actually started by returning a Stream object from the server and you're right -- the datatable bound well, however I couldn't get the server to wrap the json in the jsonP padding. It seemed that wcf binding will only spit out jsonp when I am returning a string.

    So no way to make datatables work with a string?

    Thanks again!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    > So no way to make datatables work with a string?

    It expects an object. You could use something like:

    [code]
    "success": function ( text ) {
    fnCallback( $.parseJSON( test ) );
    }
    [/code]

    Allan
  • yasvintusyasvintus Posts: 7Questions: 0Answers: 0
    That worked, thank you so much. Amazing work on the plugin!
  • yasvintusyasvintus Posts: 7Questions: 0Answers: 0
    Decided to add this response for the benefit of anybody else who may be struggling with the same issue. If you'd rather have the WCF call return a Stream object containing the jsonp, like datatables expects it to, you can manually overcome the limitation of WCF that only automatically pads strings. Here is how:


    http://social.msdn.microsoft.com/Forums/vstudio/en-US/b490fb20-6418-469d-8957-4ccf87fba0ff/endpoint-encoding
This discussion has been closed.