0

This should be a simple fix, but I just have not been able to find anything about it.

I am using both postData and editData to POST a variable to the server for form editing. This variable is used in a switch to select the appropriate function. This php contains ALL of the functions for the project. I want to avoid having many different php pages.

So all of that is fine, but I cannot find a way to do the same thing for dataUrl. The one lead I've been able to find is using ajaxSelectOptions, specifically the data option. If this is the appropriate way to go about this, what is the way to use it? Like this?:

ajaxSelectOptions:{
    contentType: "application/json",
    dataType:'json',
    type:'POST',
    action: function(){ 
        return 'popCodeAdjust';
    }
}
Igor Jerosimić
  • 13,621
  • 6
  • 44
  • 53
user
  • 619
  • 8
  • 17

3 Answers3

1

In general you can use data property of ajaxSelectOptions. The code cam look like

ajaxSelectOptions: {
    type: "POST",
    data: {
        action: "popCodeAdjust";
    }
}

or

ajaxSelectOptions: {
    type: "POST",
    data: {
        action: function () {
            return "popCodeAdjust";
        }
    }
}

See here or here.

The problem can be if you really need to send the data in JSON format. In the case you can need either to serialize the value of the parameter data (like JSON.stringify({action: actionValue})) or the value with parameter name (like action: JSON.stringify(actionValue)). See the answer which role play BodyStyle attribute (WebMessageBodyStyle.Wrapped, WebMessageBodyStyle.WrappedResponse etc) in WCF method in the case.

In jqGrid 4.4.2 or higher (see the answer, my pull request and the fix) you can use postData as function. You can define it either inside of ajaxSelectOptions

ajaxSelectOptions: {
    contentType: "application/json",
    dataType: "json",
    type: "POST",
    postData: function (rowid, value, name) {
        return JSON.stringify({action: "popCodeAdjust"});
        //or depend on the relinquishment of the server side
        //return {action: JSON.stringify("popCodeAdjust")});
    }
}

You can specify postData alternatively inside of editoptions (see here).

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I was close enough to taste it, but no cigar. I just had to take out contentType and dataType (json format was not a requirment, just what I had seen other people do). Thanks Oleg. – user Feb 19 '13 at 13:18
0

How about?

$.ajax({
    type: 'POST',
    url: 'url',
    data: { varName: JSON.stringify(arrayValues) },
    dataType: 'json',
    success: function(msg) {...},
    error: function(res, status, exeption) {...}
});

Server-side:

$var = json_decode($_POST['varName'], true);
0

Inside your grid setup it would be:

postData: { KeyName: KeyValue },

You will see this extra parameter go out with your POST.

The example below will set the postData value, (if it was to change) and then trigger a reload of the grid.

$('#gridName').jqGrid('setGridParam', { postData: { UserName: userName }).trigger('reloadGrid', [{ page: 1}]);
Mark
  • 3,123
  • 4
  • 20
  • 31