使用Ajax将JSON发送到WCF 3.5
我在将JSON传递给Weight方法时遇到问题。我一直收到使用Ajax将JSON发送到WCF 3.5,ajax,wcf,json,Ajax,Wcf,Json,我在将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 { [Servi
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,
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;
}
}
计算器.svc
:
<%@
ServiceHost
Language="C#"
Debug="true"
Service="XXX.SharePoint.WebServices.Calculators"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
CodeBehind="Calculators.svc.cs"
%>
在同一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
。您发送的请求使用了表单/url编码的内容类型(当data
成员的值是对象时,jQuery.ajax的默认值),WCF默认不支持。您可以按照Darin Dimitrov的建议将其更改为发送JSON,将服务操作更改为将流作为参数并自己解析输入,或者扩展WCF以支持表单/url编码的数据。我在另一个问题(在)中发布了一个示例,该问题是后者
另外,中的“jQuery支持”预览有一些代码也支持该内容类型。太棒了!!这帮了大忙!“.svc文件中使用的特殊WebServiceHostFactory”为我做到了:)
$.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);
}
});