我在将JSON传递给Weight方法时遇到问题。我一直在找HTTP/1.1 415 Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'.
我想我的合同或web.config都有问题。我所有的研究结果都是空的。我将使用jQuery的$.ajax从Web部件调用此服务。
接口:
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);
    }
}web.config:
<?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 information, 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 information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>一如既往,提前感谢!
发布于 2011-07-08 05:56:24
以下是IIS中托管的WCF服务的完整工作示例:
[ServiceContract]
public interface ICalculators
{
    [OperationContract]
    [WebInvoke(Method = "POST",
               BodyStyle = WebMessageBodyStyle.Wrapped,
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json
    )]
    float Weight(float width, float diameter, float size, float factor);
}
public class Calculators : ICalculators
{
    public float Weight(float width, float diameter, float size, float factor)
    {
        return 10f;
    }
}calculators.svc
<%@ 
    ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="XXX.SharePoint.WebServices.Calculators" 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory"
    CodeBehind="Calculators.svc.cs" 
%>web.config:
<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>  在同一个ASP.NET应用程序中使用jQuery消费:
$.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);
    }
});注意在web.config中使用webHttpBinding而不是basicHttpBinding (SOAP),以及.svc文件中使用的特殊WebServiceHostFactory。
发布于 2011-07-08 06:25:22
您发送的请求使用forms/url编码的内容类型(当data成员的值是对象时,这是jQuery.ajax的默认类型),这是data默认不支持的。您可以按照Darin Dimitrov的建议将其更改为发送JSON,将您的服务操作更改为接受Stream作为参数并自己解析输入,或者扩展WCF以支持表单/url编码的数据。我已经在另一个问题(在RESTful WCF web service POST problem上)中发布了一个示例,它完成了后者。
此外,来自http://wcf.codeplex.com的"jQuery支持“的预览中也有一些支持该内容类型的代码。
https://stackoverflow.com/questions/6617387
复制相似问题