加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Ajax将JSON发送到WCF 3.5

发布时间:2020-12-16 02:50:17 所属栏目:百科 来源:网络整理
导读:我在将 JSON传递给Weight方法时遇到问题.我一直得到HTTP / 1.1 415无法处理消息因为内容类型’application / x-www-form-urlencoded; charset = UTF-8’不是预期的类型’text / xml;字符集= UTF-8′ . 我想我的合同或web.config都有问题.我所有的研究都是空
我在将 JSON传递给Weight方法时遇到问题.我一直得到HTTP / 1.1 415无法处理消息因为内容类型’application / x-www-form-urlencoded; charset = UTF-8’不是预期的类型’text / xml;字符集= 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>

一如既往,提前谢谢!

解决方法

以下是IIS中托管的WCF服务的完整工作示例:

[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;
    }
}

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读