![](/img/trans.png)
[英]POST request to WCF REST (json) - WebException (400) bad request
[英]jqGrid, POST returning bad request using WCF with REST
我正在使用jqGrid來顯示服務的數據,我以前使用jsonp
檢索數據,但現在不允許使用它,因此我更改了該應用程序的所有其他ajax調用(它們使用jQuery ajax),並且它們工作正常,但是jqGrid給我帶來了問題,以前我使用過:
致電服務的先前方法:
myObj.jqGrid({
url: externalUrlOfMyService,
datatype: 'json',
mtype: 'GET',
postData: {
obj1: JSON.stringify(valueObj1),
obj2: JSON.stringify(valueObj2)
})
...
});
最新的不可行的服務調用方式:
myObj.jqGrid({
url: externalUrlOfMyService,
datatype: 'json',
mtype: 'POST',
postData: JSON.stringify({
obj1: valueObj1,
obj2: valueObj2
}),
ajaxGridOptions: { contentType: "application/json", cache: true },
...
})
我知道它到達了服務器,因為我在global.asax
的Application_BeginRequest
上有一個斷點,但僅此而已,它從未輸入方法。
如果我嘗試使用GET
方法,它將進入該方法,但所有參數均為null
。
注意:我正在使用jqGrid 4.5.2
有任何想法嗎?
提前致謝。
更新
<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)
更新2
我這樣做了,但是我的要求很差,有什么問題嗎?
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
});
},
更新3
提琴手
更新4
也許無關緊要,但這是我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
更新5
我只是檢查一下jqGrid選項的末尾是否有ajaxGridOptions: { timeout: 30000 }
,它覆蓋了我先前設置的ajaxGridOptions: { contentType: "application/json" }
,因此,我具有Content-Type: text/html
提琴手上的Content-Type: text/html
並給了我400錯誤的要求。
您使用BodyStyle:=WebMessageBodyStyle.WrappedRequest
屬性。 這意味着所有參數一起需要通過JSON編碼為一個對象。 看到答案 。
因此,您必須使用serializeGridData
來序列化所有參數 (您在postData
和標准jqGrid參數中添加的自定義參數):
serializeGridData: function (data) {
return JSON.stringify(data);
}
我建議您另外刪除cache: true
選項,這在使用mtype: 'POST'
情況下是沒有意義mtype: 'POST'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.