发送JSON使用Ajax WCF 3.5 [英] Send JSON to WCF 3.5 using Ajax
问题描述
我在传递JSON的重量法的问题。我不断收到 HTTP / 1.1 415无法处理邮件,因为内容类型应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8是不是预期的类型为text / xml;字符集= UTF-8。
I'm having issues passing JSON to the Weight method. I keep getting 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中的一个问题。我所有的研究变成了空。我会打电话从Web部件使用此服务jQuery的$。阿贾克斯。
I think I have a problem with either my contract or web.config. All my research turns up empty. I'll be calling this service from a Web Part using jQuery's $.ajax.
接口:
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中:
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>
和往常一样,在此先感谢!
As always, thanks in advance!
推荐答案
下面是在IIS中承载WCF服务的一个完整的工作的例子:
Here's a full working example of a WCF service hosted in IIS:
[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功耗:
Consumption using jQuery in the same ASP.NET application:
$.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);
}
});
注意的使用的WebHttpBinding
而不是 basicHttpBinding的
(SOAP)在web.config中还有特殊的 WebServiceHostFactory
在 .SVC
文件中使用。
Notice the usage of webHttpBinding
instead of basicHttpBinding
(SOAP) in web.config as well as the special WebServiceHostFactory
used in the .svc
file.
这篇关于发送JSON使用Ajax WCF 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!