[英]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: 我正在使用jqGrid来显示服务的数据,我以前使用
jsonp
检索数据,但现在不允许使用它,因此我更改了该应用程序的所有其他ajax调用(它们使用jQuery ajax),并且它们工作正常,但是jqGrid给我带来了问题,以前我使用过:
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. 我知道它到达了服务器,因为我在
global.asax
的Application_BeginRequest
上有一个断点,但仅此而已,它从未输入方法。
If I try with GET
method it enters the method but all parameters are null
. 如果我尝试使用
GET
方法,它将进入该方法,但所有参数均为null
。
Note: I'm using jqGrid 4.5.2 注意:我正在使用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 更新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 更新3
Fiddler 提琴手
UPDATE 4 更新4
Maybe it's irrelevant, but heres the info of my global.asax: 也许无关紧要,但这是我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 更新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. 我只是检查一下jqGrid选项的末尾是否有
ajaxGridOptions: { timeout: 30000 }
,它覆盖了我先前设置的ajaxGridOptions: { contentType: "application/json" }
,因此,我具有Content-Type: text/html
提琴手上的Content-Type: text/html
并给了我400错误的要求。
You use BodyStyle:=WebMessageBodyStyle.WrappedRequest
proeprty. 您使用
BodyStyle:=WebMessageBodyStyle.WrappedRequest
属性。 It means that all parameters together needed be JSON encoded as one object. 这意味着所有参数一起需要通过JSON编码为一个对象。 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
来序列化所有参数 (您在postData
和标准jqGrid参数中添加的自定义参数):
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'
. 我建议您另外删除
cache: true
选项,这在使用mtype: 'POST'
情况下是没有意义mtype: 'POST'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.