24

I am using the latest version of jqGrid: 3.6.4

This seems like a simple problem (or at least it did before I spent a few hours on it):

When the grid sends a request to the server (to a controller action), its content-type is always:

application/x-www-form-urlencoded; charset=UTF-8

and I would like it to be:

application/json; charset=utf-8

but I can find no way of setting the content-type (there is no contentType option as you would find on a $.ajax call for example).

So just to clarify, I am not asking how to set the content-type on a jQuery server request, but specifically using jqGrid, which does not provide an obvious option for doing this.

Thanks, Nigel.

Update: Oleg's response fixed solved it.

Here are the option settings for the grid:

jQuery("#ContactGridList").jqGrid({
        url: '/ContactSelect/GridData/',
        datatype: 'json',
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        mtype: 'POST',
        ...
Nigel
  • 2,150
  • 3
  • 20
  • 22

1 Answers1

27

How you can find in the code of grid.base.js the $.ajax call filling the grid contain looks like following:

$.ajax($.extend({
    url: ts.p.url,
    type: ts.p.mtype,
    dataType: dt,
    data: $.isFunction(ts.p.serializeGridData) ?
             ts.p.serializeGridData.call(ts, ts.p.postData) : ts.p.postData,
    complete: function (req, st) {
       ...
    }
    ...
}, $.jgrid.ajaxOptions, ts.p.ajaxGridOptions));

So you can use ajaxGridOptions option of jqGrid to set or override any parameter of $.ajax request. Because I use only JSON requests to my server, I set general setting of contentType like

$.extend($.jgrid.defaults, {
    datatype: 'json',
    {ajaxGridOptions: { contentType: "application/json" },
    {ajaxRowOptions: { contentType: "application/json", type: "PUT" },
    ...
});

The ajaxRowOptions are used in grid.inlinedit.js for row editing. For the form edit there are other parameters, which I set also as global setting:

$.extend($.jgrid.edit, {
    ajaxEditOptions: { contentType: "application/json" },
    ...
});

$.extend($.jgrid.del, {
    ajaxDelOptions: { contentType: "application/json" },
    mtype: "DELETE",
    ...
});

How you can see my server is a RESTfull service (developed mainly in WFC and the rest in ASP.NET MVC). Because $.jgrid.edit is a setting for both "add" and "modify" items, I could not change mtype: "PUT" for "edit" only, so I do this in parameters of navGrid().

The last ajax parameter which you could find also interesting to set is ajaxSelectOptions. You can set it on the same way as ajaxGridOptions. Parameters of ajaxSelectOptions are useful if you use dataUrl parameter inside of editoptions or searchoptions. I use, for example, dataUrl inside of colModel for defining columns of the type edittype: 'select'. The possible values of select option will be loaded from server for inline or form editing or inside of search dialog. Because for such data loading are used ajax, there is corresponding ajaxSelectOptions option.

Best regards.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg! That did it; I added my example code to the question. – Nigel Apr 20 '10 at 23:13
  • Very strange! I use also jqGrid 3.6.4. Because I use jQuery 1.4.2 I have to use the version from http://github.com/tonytomov/jqGrid, but ajax features exists start from 3.6 (3.6.0). You can search for ajaxGridOptions in you version of jqGrid to verify it. I suggest you load the last uncompressed version of jqGrid from http://github.com/tonytomov/jqGrid, modify you code to use this js-files (grid.loader.js for example), set breakpoints inside grid.base.js (search for .ajax) and inside of jQuery.ajax (also uncompressed jquery-1.4.2.js). You can also make one test with setting $.jgrid.defaults. – Oleg Apr 20 '10 at 23:20
  • @vissupepala: You are welcome one more time! Another callback `beforeProcessing` come later, but it can be also very helpful in some scenarios. It allows to change the server response in any way *before* it will be processed by jqGrid. – Oleg Jun 18 '12 at 19:03
  • @Oleg, serializeGridData is not called with the postData when adding a row with the form. Has it changed since this message ? serializeDelData is not called either. – singe3 Nov 17 '15 at 10:39
  • @singe3: Sorry, I can't follow you . It's better that you describe detailed what you do and what problem you have. `postData` parameter and `serializeGridData` callback of jqGrid allows to extend the parameters posted to the server for **filling the grid**. `serializeDelData` callback of **form editing** will be called by [delGridRow](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing#delgridrow) method which you could call directly or indirectly. You can add additional data posted during deleting the row. One have to know how you use `delGridRow` and which version of jqGrid. – Oleg Nov 17 '15 at 10:46
  • @Oleg I'm just looking for the callback which is called when adding a row through the navigator form but I can't find it, serializeGridData is only called when paginating or searching – singe3 Nov 17 '15 at 10:59
  • @singe3: Sorry, but all what you write I can interpret **in many ways**. I have to guess what you do, what jqGrid version you use, how you use it and what problem you could have. It would be better if you post new separate question where you describe clear and detailed what you do and what problem you have. It would be speed up if you create JSFiddle demo (or another demo) which demonstrates what you do. One could just make some changes in the code to show how one can solve your problems. – Oleg Nov 17 '15 at 11:31
  • @Oleg I understand, my question wasn't clear. Anyway I finally managed to make it work. I found that the "add" options in the navGrid need to use `ajaxEditOptions` and `serializeEditData` . It confused me, I was trying to use `ajaxAddOptions` and `serializeAddData` which do not not exist. – singe3 Nov 17 '15 at 13:56