简体   繁体   中英

Recieving the edit/add data from jqGrid in x-www-form-urlencoded, i need application/json

I am trying to link my jqGrid to my Java Rest service. When i send the edit/add data from jqGrid to my rest in order to process it for DB CRUD operations, i recieve it like this :

"nume=Alin&prenume=Dan&sefDepartament=Yes&position=position+2&joinYear=2015-08-11&oper=edit&id=2"

I need to recieve it in a JSON format. How can i achieve that?

This is my Java piece of code :

@Path("/update")
public class UpdateDatabase {

@POST
public String updateDatabase(String data){

    System.out.println(data);
}

This is my jqGrid settings i have regarding the server connection:

 grid.jqGrid({
    pager: '#pager', 
    mtype: "POST",
    url: "/RestWithDatabaseConnection/rest/fetchData",
    editurl:'/RestWithDatabaseConnection/rest/update',
    datatype: "json",

Please let me know if i should add some other information.

To send the results of editing encoded in JSON format one should

  1. set Content-Type: application/json in the HTTP header
  2. use JSON.stringify to encode the posted data

If you use free jqGrid then you can use the following inlineEditing option, which are the options of jqGrid (in the same level where editurl )

inlineEditing: {
    ajaxSaveOptions: { contentType: "application/json" },
    serializeSaveData: function (postData) {
        return JSON.stringify(postData);
    }
}

In case of usage old jqGrid versions you can use jqGrid options ajaxRowOptions and serializeRowData in almost the same way:

ajaxRowOptions: { contentType: "application/json" },
serializeRowData: function (postData) {
    return JSON.stringify(postData);
}

Free jqGrid supports jqGridInlineSerializeSaveData event as alternative to serializeSaveData or serializeRowData callback. It's helpful in more advances scenarios. The options ajaxRowOptions and serializeRowData are supported by free jqGrid too because of the compatibility reasons.

I would recommend you to consider to use free jqGrid 4.12.0 or the latest source from GitHub. The list on main new features and the bug fixes is described in the readme as usually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM