我正在尝试从我的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服务以使其接受
不是…而是
当我使用 JQuery 进行测试时,发送了一个使用 URL 编码的 Ajax 请求(如下所示):
请注意,由于编码的差异,此代码确实有效。
感谢任何帮助!
尽管似乎有几种选项可以返回给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>
感谢任何帮助!