【问题标题】:Send JSON to WCF 3.5 using Ajax使用 Ajax 将 JSON 发送到 WCF 3.5
【发布时间】:2011-09-30 20:04:42
【问题描述】:

我在将 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>

一如既往,提前致谢!

【问题讨论】:

    标签: ajax wcf json


    【解决方案1】:

    您发送的请求使用表单/url 编码的内容类型(当 data 成员的值是对象时,jQuery.ajax 的默认值),WCF 默认不支持。您可以更改它以按照 Darin Dimitrov 的建议发送 JSON,更改您的服务操作以将 Stream 作为参数并自己解析输入,或者扩展 WCF 以支持表单/url 编码的数据。我在另一个问题(RESTful WCF web service POST problem)中发布了一个样本,后者是后者。

    此外,http://wcf.codeplex.com 的“jQuery 支持”预览也有一些支持该内容类型的代码。

    【讨论】:

      【解决方案2】:

      这是托管在 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

      【讨论】:

      • 太棒了!!这帮助很大!
      • “.svc 文件中使用的特殊 WebServiceHostFactory”为我做了:)
      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2016-11-11
      • 1970-01-01
      相关资源
      最近更新 更多