jqGrid,POST 使用 WCF 和 REST 返回错误请求
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" }
,因此我在 fiddler 上有 Content-Type: text/html
并给我 400 个错误的请求。
您使用 BodyStyle:=WebMessageBodyStyle.WrappedRequest
财产。这意味着 所有参数一起 需要 JSON 编码为一个对象。参见 the answer。
因此你必须使用serializeGridData
序列化所有参数(你在postData
中添加的自定义参数和标准jqGrid参数):
serializeGridData: function (data) {
return JSON.stringify(data);
}
我建议您另外删除 cache: true
选项,这在使用 mtype: 'POST'
.
的情况下没有任何意义
我正在使用 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" }
,因此我在 fiddler 上有 Content-Type: text/html
并给我 400 个错误的请求。
您使用 BodyStyle:=WebMessageBodyStyle.WrappedRequest
财产。这意味着 所有参数一起 需要 JSON 编码为一个对象。参见 the answer。
因此你必须使用serializeGridData
序列化所有参数(你在postData
中添加的自定义参数和标准jqGrid参数):
serializeGridData: function (data) {
return JSON.stringify(data);
}
我建议您另外删除 cache: true
选项,这在使用 mtype: 'POST'
.