我正在尝试从我的 ASP.NET 2.0 WebForms 应用程序中运行的 WCF Web 服务获取 JQGrid 的数据。问题是 WCF Web 服务希望将数据格式化为 JSON 字符串,而 JQGrid 正在执行 HTTP Post 并将其作为 Content-Type: application/x-www-form-urlencoded 传递。
尽管返回给 JQGrid 的数据格式似乎有多种选择(它接受 JSON、XML 和其他格式),但似乎没有办法改变将输入传递给 Web 服务的方式。
所以我试图弄清楚如何调整 WCF 服务以便它接受
Content-Type: application/x-www-form-urlencoded
而不是
Content-Type:"application/json; charset=utf-8"
当我使用 JQuery 进行测试以使用 url 编码发送 Ajax 请求时(如图所示):
$.ajax({
type: "POST",
url: "../Services/DocLookups.svc/DoWork",
data: 'FirstName=Howard&LastName=Pinsley',
contentType: "Content-Type: application/x-www-form-urlencoded",
dataType: "json",
success: function(msg) {
alert(msg.d);
}
});
通话失败。使用 Fiddler 检查流量,我发现服务器返回的错误:
{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":
"The incoming message has an unexpected message format 'Raw'. The expected
message formats for the operation are 'Xml', 'Json'. This can be because
a WebContentTypeMapper has not been configured on the binding.
See the documentation of WebContentTypeMapper for more details."...
请注意,此代码确实有效,因为编码不同
$.ajax({
type: "POST",
url: "../Services/DocLookups.svc/DoWork",
data: '{"FirstName":"Howard", "LastName":"Pinsley"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
}
});
在服务器上,服务如下所示:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class DocLookups {
// Add [WebGet] attribute to use HTTP GET
[OperationContract]
public string DoWork(string FirstName, string LastName) {
return "Your name is " + LastName + ", " + FirstName;
}
}
我的 web.config 包含:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="DocLookupsAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="DocLookups">
<endpoint address="" behaviorConfiguration="DocLookupsAspNetAjaxBehavior"
binding="webHttpBinding" contract="DocLookups" />
</service>
</services>
</system.serviceModel>
谢谢你的帮助!