user123456
user123456

Reputation: 133

jqGrid, POST returning bad request using WCF with REST

I'm using jqGrid to show the data of my service, I was previously retrieving the data using jsonp but I'm not allow to use it anymore, so I changed all the others ajax calls of the application (they use jQuery ajax), and they work fine but jqGrid is giving me problems, previously I was using:

Previous way to call the service:

myObj.jqGrid({
    url: externalUrlOfMyService,
    datatype: 'json',
    mtype: 'GET',
    postData: {
        obj1: JSON.stringify(valueObj1),
        obj2: JSON.stringify(valueObj2)
    })
    ...
});

Newest and not working way of calling the service:

myObj.jqGrid({
    url: externalUrlOfMyService,
    datatype: 'json',
    mtype: 'POST',
    postData: JSON.stringify({
        obj1: valueObj1,
        obj2: valueObj2
    }),
    ajaxGridOptions: { contentType: "application/json", cache: true },
    ...
})

I know it reaches the server cause I have a breakpoint on the Application_BeginRequest of my global.asax, but that's all, it never enters the method.

If I try with GET method it enters the method but all parameters are null.

Note: I'm using jqGrid 4.5.2

Any ideas?

Thanks in advance.

UPDATE

<OperationContract()>
<WebInvoke(Method:="*",
           RequestFormat:=WebMessageFormat.Json,
           ResponseFormat:=WebMessageFormat.Json,
           BodyStyle:=WebMessageBodyStyle.WrappedRequest)>
<FaultContract(GetType(ServiceFault))>
Function RetrievePhones(userCompany As LookUpValue,
                    recordOwnerType As Enumerations.EnumRecordOwnerType,
                    recordOwnerId As String,
                    consumer As ConsumerObject)

UPDATE 2

I did this, but I'm getting bad request, is there something wrong?

postData: {
    userCompany: userCompany,
    recordOwnerType: recordOwnerType,
    recordOwnerId: recordOwnerId,
    consumer: consumer
},
ajaxGridOptions: { contentType: "application/json" },
serializeGridData: function (data) {
    return JSON.stringify({
        userCompany: data.userCompany,
        recordOwnerType: data.recordOwnerType,
        recordOwnerId: data.recordOwnerId,
        consumer: data.consumer
    });
},

UPDATE 3

Fiddler

enter image description here

UPDATE 4

Maybe it's irrelevant, but heres the info of my global.asax:

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", WebConfigurationManager.AppSettings("AllowOrigin"))
'HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true")
'AuthenticateAJAXRequest()
If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then
    'These headers are handling the "pre-flight" OPTIONS call sent by the browser
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST")
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With")
    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000")
    HttpContext.Current.Response.[End]()
End If

UPDATE 5

I just check that at the end of the options of my jqGrid I had ajaxGridOptions: { timeout: 30000 } which was overwriting ajaxGridOptions: { contentType: "application/json" } that I was previously setting, therefore I was having Content-Type: text/html on fiddler and giving me the 400 bad request.

Upvotes: 0

Views: 344

Answers (1)

Oleg
Oleg

Reputation: 221997

You use BodyStyle:=WebMessageBodyStyle.WrappedRequest proeprty. It means that all parameters together needed be JSON encoded as one object. See the answer.

Thus you have to use serializeGridData to serialize all parameters (your custom parameters which you added in postData and the standard jqGrid parameters):

serializeGridData: function (data) {
    return JSON.stringify(data);
}

I recommend you to remove cache: true option additionally, which have no sense in case of usage mtype: 'POST'.

Upvotes: 1

Related Questions