views:

298

answers:

1

I've got a jqGrid setup to post to a URL using the content type of application/json:

$("#jqCategoryGrid").jqGrid({
    datatype: "json",
    mtype: 'POST',        
    url: "Webservices/TroubleTicketCategory.asmx/getCategoryData",
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    // **UPDATE - This is the fix, as per Oleg's response**
    serializeGridData: function (postData) {
        if (postData.searchField === undefined) postData.searchField = null;
        if (postData.searchString === undefined) postData.searchString = null;
        if (postData.searchOper === undefined) postData.searchOper = null;
        //if (postData.filters === undefined) postData.filters = null;
        return JSON.stringify(postData);
    },
});

The problem is that the jqGrid is still trying to pass parameters using a non-json format so I'm getting an error "Invalid JSON Primitive"

Is there a way to instruct the jqGrid to serialize the data using Json?

Thanks

UPDATE

I edited the provided source code in my question to include the fix I used, which came from Oleg's response below.

+1  A: 

You should include JSON serialization of the posted data for example with respect of json2.js which can be downloaded from http://www.json.org/js.html:

serializeRowData: function (data) { return return JSON.stringify(data); }
Oleg
How do I do this? From all I can see, the jqGrid is black-boxed and handles all data serialization in the background. Is there a method or property or something I can specify in the init of the grid that instructs it to do this?
Chu
I don't understand your question. In http://stackoverflow.com/questions/2675625/setting-the-content-type-of-requests-performed-by-jquery-jqgrid/2678731#2678731 I showed a code fragment of jqGrid which use `serializeRowData`. With respect of this function you have FULL control under the data sent to server. You have as `data` an object with data which will be send and you can convert there in a string how you want without restrictions. Moreover the full code of jqGrid you can free download and study some other features. So it is not a black-box. It is an open box I see.
Oleg
The referenced URL you sent contains the answer. I'll update my post to reflect this.One other question - is there a way to get my form edit to serialize the data? It doesn't appear to use the same "serializeGridData" as the main loading method uses.
Chu
To do this you should use `serializeEditData: function (data) { return return JSON.stringify(data); }`. You can use `jQuery.extend(jQuery.jgrid.edit, {ajaxEditOptions: { contentType: "application/json" }, serializeEditData: function (data) { return return JSON.stringify(data); }});` to ovewrite this parameters for all jqGrids
Oleg