How to use the server-side callback for post-processing?

How to use the server-side callback for post-processing?

MorriganMorrigan Posts: 10Questions: 0Answers: 0
edited September 2010 in General
Hello,

Maybe I'm missing something obvious here, but I am confused as to how I could use the fnServerData callback function to post-process data received from the server. In the example we have this, which does *pre* processing:

[code]/* POST data to server */
"fnServerData": function ( sSource, aoData, fnCallback ) {
/* Add some data to send to the source, and send as 'POST' */
aoData.push( { "name": "my_field", "value": "my_value" } );
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
[/code]

But basically, want I want to do here, is override the "fnCallback" function. I want to be able to analyse the json data returned from the ajax call, and call a specific function if there's only one row in the data. So I tried this:

[code]
fnServerData: function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success": function(result){
if (result.numRows == 1) {
callMyCustomFunc();
} else {
fnCallback(); // draw the table
}
}
} );
}
[/code]

But that doesn't work. I stripped away my custom code, and tried to just leave "fnCallback()", but I get a Javascript error saying "b is undefined".

What am I doing wrong, exactly?

Thanks for any help.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I think you are very close with your code there, you just need to pass the "result" variable to fnCallback (i.e. fnCallback( result ); - so giving DataTables the result set that it expects.

    Allan
  • MorriganMorrigan Posts: 10Questions: 0Answers: 0
    Ohhhh, wow. I can't believe I missed this detail. How embarrassing.

    Thanks a lot, allan!
This discussion has been closed.