0

I am trying to use serializeGridData to convert my postdata to JSON using this answer from Oleg. Here is my code.

jQuery(function() {
   $('#grid').jqGrid({
       ........
       ........
       postData: {
        param1: function() { return $("param1").val(); },
        param2: function() { return $("param2").val(); },
        searchText: function() { return $("searchText").val(); },
        totalRecords: function() { return msgGrid.getGridParam("records"); }
    },
    serializeGridData: function (postData) {
           return JSON.stringify(postData);
    },
    ajaxGridOptions : {
        contentType: 'application/json; charset=utf-8'
    }
    });
});

postData coming to serializeGridData does not replace the custom params defined in postData {} with actual values. I have debugged in firebug and this is how the data is coming to serializeGridData method. So eventually JSON.stringify is not converting param1, param2.. values in the request data. How can fix it to send the actual values for custom params? Thanks in advance..

_search     false
nd      1349195468864
page        1
rows        25
sidx        "sortcol_name"
sord        "desc"
param1      function()        // expect to have actual param1 value
param2      function()
searchText          function()
totalRecords    function()
Community
  • 1
  • 1
varaprakash
  • 487
  • 4
  • 12
  • 30

2 Answers2

0

When JSON.stringify encounters a function, it censors it to null. Try setting the data without functions, like this:

postData: {
    param1: $("param1").val(),
    // etc.
}
Owlvark
  • 1,763
  • 17
  • 28
0

I think you should enumerate all properties of postData inside of serializeGridData callback and use $.isFunction inside of it to verify whether some property is a method. In case of method you should call the method and save the result in the property with the same name. Look at the implementation of serializeRowData from the answer or in the code of the answer.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • That worked great. Thanks a lot, you are so helpful. I was just making the changes when you put the comment about if I have tried this answer. Thank you again!! – varaprakash Oct 02 '12 at 19:03