gpt4 book ai didi

ajax - 使用 Ajax 将 JSON 发送到 WCF 3.5

转载 作者:行者123 更新时间:2023-12-04 23:29:57 24 4
gpt4 key购买 nike

我在将 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'.
我想我的契约(Contract)或 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);
}
}

网络配置:
<?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;
}
}
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);
}
});

注意 webHttpBinding的用法而不是 basicHttpBinding (SOAP) 在 web.config 以及特殊的 WebServiceHostFactory用于 .svc文件。

关于ajax - 使用 Ajax 将 JSON 发送到 WCF 3.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6617387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com