我在将
JSON传递给Weight方法时遇到问题.我一直得到HTTP / 1.1 415无法处理消息因为内容类型’application / x-www-form-urlencoded; charset = UTF-8’不是预期的类型’text / xml;字符集= UTF-8′ .
@H_404_5@
@H_404_5@我想我的合同或web.config都有问题.我所有的研究都是空洞的.我将使用jQuery的$.ajax从Web部件调用此服务.
@H_404_5@接口:
@H_404_5@
namespace XXX.SharePoint.WebServices { [ServiceContract] public interface ICalculators { [OperationContract] [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = Webmessageformat.Json,ResponseFormat = Webmessageformat.Json )] Single Weight(Single Width,Single Diameter,Single Size,Single Factor); } }@H_404[email protected]fig中: @H_404_5@
<?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service behaviorConfiguration="XXX.SharePoint.WebServices.CustomServiceBehavIoUr" name="XXX.SharePoint.WebServices.Calculators"> <endpoint address="" binding="basicHttpBinding" contract="XXX.SharePoint.WebServices.ICalculators" > <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://moss2010/"></add> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="XXX.SharePoint.WebServices.CustomServiceBehavIoUr"> <!-- To avoid disclosing Metadata @R_802_4045@ion,set the value below to false and remove the Metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes,set the value below to true. Set to false before deployment to avoid disclosing exception @R_802_4045@ion --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>@H_404_5@一如既往,提前谢谢!
解决方法
以下是IIS中托管的WCF服务的完整工作示例:
@H_404_5@
@H_404_5@
[ServiceContract] public interface ICalculators { [OperationContract] [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = Webmessageformat.Json )] float Weight(float width,float diameter,float size,float factor); } public class Calculators : ICalculators { public float Weight(float width,float factor) { return 10f; } }@H_404_5@calculators.svc: @H_404_5@
<%@ ServiceHost Language="C#" Debug="true" Service="XXX.SharePoint.WebServices.Calculators" Factory="System.ServiceModel.Activation.WebServiceHostFactory" CodeBehind="Calculators.svc.cs" %>@H_404[email protected]fig中: @H_404_5@
<system.serviceModel> <services> <service behaviorConfiguration="XXX.SharePoint.WebServices.CustomServiceBehavIoUr" name="XXX.SharePoint.WebServices.Calculators"> <endpoint address="" binding="webHttpBinding" contract="XXX.SharePoint.WebServices.ICalculators" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="XXX.SharePoint.WebServices.CustomServiceBehavIoUr"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>@H_404_5@在同一个ASP.NET应用程序中使用jQuery消耗: @H_404_5@
$.ajax({ url: '/calculators.svc/Weight',type: 'POST',contentType: 'application/json',data: JSON.stringify({ Width: 1.2,Diameter: 2.3,Size: 3.4,Factor: 4.5 }),success: function (result) { alert(result.WeightResult); } });@H_404_5@请注意web.config中使用webHttpBinding而不是basicHttpBinding(SOAP)以及.svc文件中使用的特殊WebServiceHostFactory.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。