使用带有json和ajax的wcf时出现405错误

时间:2014-04-15 14:01:26

标签: c# javascript json wcf

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

认为之前的答案(关于jsonp)是有道理的,但如果它不起作用,请尝试添加到&#34; customHeaders&#34;

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