405将wcf与json和ajax一起使用时出错

本文关键字:一起 出错 ajax wcf json | 更新日期: 2023-09-27 17:58:21

我正在尝试构建一个WCF服务,该服务接受并返回json格式的数据,并在ajax调用中使用它。我遇到的问题是,当我试图从javascript调用WCF服务时,我得到了一个405(方法不允许)错误。网页似乎调用了服务器上的选项方法(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')); }
    });

405将wcf与json和ajax一起使用时出错

你们在同一个域名上吗?如果没有,请使用"jsonp"作为dataType,而不是

认为前面的答案(关于jsonp)是有意义的,但如果它不起作用,请尝试添加到"customHeaders"

<add name="Access-Control-Allow-Methods" value="GET,POST" />