How to use fnServerData to modify the server response?

How to use fnServerData to modify the server response?

nmendozanmendoza Posts: 3Questions: 1Answers: 0

I want to make some validation of my json object this is my code:

$(document).ready( function() {
    $('#table').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "columns": [{ "data": "id" },{ "data": "codigo" },{"data": "nombre" },{ "data": "pais" }],
    "fnServerData": function ( sSource, aoData, fnCallback) {
        sSource= "/gdoc/abm/ciudad/rest";
        $.getJSON( sSource, aoData, function (json) {
            jQuery.each(json.aaData, function(index, item) {
                jQuery.each( item, function( key, value ) {
                           //some conditions  
                                   item.key = "";
                 });
            });
            fnCallback(json);
        });
    }
    });
});

But the Datatable show the json without any modification. What it's wrong with this code?

Answers

  • nmendozanmendoza Posts: 3Questions: 1Answers: 0

    I also try this:

    $(document).ready( function() {
        var tabla = $('#table').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/gdoc/abm/ciudad/rest",
        "columns": [{ "data": "id" },{ "data": "codigo" },{"data": "nombre" },{ "data": "pais" }],
        "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
              oSettings.jqXHR = $.ajax( {
                "dataType": 'json',
                "type": "GET",
                "url": sSource,
                "data": aoData,
                "success": function (data) {
                    jQuery.each(data.aaData, function(index, item) {
                        jQuery.each( item, function( key, value ) {
                            //some conditions 
                                            item.key = "";
                         });
                    });
                    fnCallback(data);
                 }
              } );
        }
      });
    });
    
  • nmendozanmendoza Posts: 3Questions: 1Answers: 0

    What stupid, I was creating the key "key" in item. I solved it. Thanks

This discussion has been closed.