首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Ajax将JSON发送到WCF3.5

使用Ajax将JSON发送到WCF3.5
EN

Stack Overflow用户
提问于 2011-07-08 05:30:19
回答 2查看 4.9K关注 0票数 4

我在将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部件调用此服务。

接口:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
<?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>

一如既往,提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-08 05:56:24

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

代码语言:javascript
运行
复制
[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

代码语言:javascript
运行
复制
<%@ 
    ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="XXX.SharePoint.WebServices.Calculators" 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory"
    CodeBehind="Calculators.svc.cs" 
%>

web.config:

代码语言:javascript
运行
复制
<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消费:

代码语言:javascript
运行
复制
$.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

票数 7
EN

Stack Overflow用户

发布于 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支持“的预览中也有一些支持该内容类型的代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6617387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档