gpt4 book ai didi

c# - 将 wcf 与 json 和 ajax 一起使用时出现 405 错误

转载 作者:行者123 更新时间:2023-11-28 01:22:36 25 4
gpt4 key购买 nike

我正在尝试构建一个 WCF 服务,该服务接受并返回 json 格式的数据,并在 ajax 调用中使用它。我遇到的问题是,当我尝试从 javascript 调用 WCF 服务时,出现 405(方法不允许)错误。该网页似乎调用了服务器上的 options 方法 (OPTIONS localhost:49572/Service1.svc/testmethod HTTP/1.1),服务器以 405 状态码进行响应。

下面是我的服务定义

namespace JsonAjaxService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "testmethod")]
CompositeType GetDataUsingDataContract(CompositeType composite);
}

[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

}

这是 Web.config

<?xml version="1.0"?>
<configuration>

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1"/>
</system.web>
<system.serviceModel>
<services>
<service name="JsonAjaxService.Service1" behaviorConfiguration="ServiceBehaviour">
<endpoint address ="" binding="webHttpBinding" contract="JsonAjaxService.IService1" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>

<directoryBrowse enabled="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>

</configuration>

最后是我用来调用此服务的代码

function test() {
var json = '{"StringValue":"test","BoolValue":"false"}';
$.ajax(
{
type: "POST",
processData: false,
contentType: "application/json",
url: "http://localhost:49572/Service1.svc/testmethod",
data: json,
dataType: "json",
success: function (data) { alert(data); },
error: function (data) { alert('error')); }
});

最佳答案

你们在同一个域名上吗?如果没有,请使用“jsonp”作为数据类型

关于c# - 将 wcf 与 json 和 ajax 一起使用时出现 405 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23086006/

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