How to use the server-side callback for post-processing?
How to use the server-side callback for post-processing?
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.
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.
This discussion has been closed.
Replies
Allan
Thanks a lot, allan!