WCF REST服务返回405:jQuery AJAX GET不允许的方法

时间:2012-04-09 02:32:55

标签: json jquery wcf-rest http-status-code-405

即使经过这篇文章 wcf REST Services and JQuery Ajax Post: Method not allowed (3)

我无法通过405:尝试通过以下AJAX调用在我的WCF REST服务上调用方法时不允许使用方法:

$.ajax({
    type: "GET",
    url: 'http://mydomain.com/service1/service.svc/json/getsupportedagencies',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    failure: function (msg) {
        alert(msg);
    },
    success: function (agencies) {
        alert("success via REST");
        $.each(agencies, function (i, a) {
            viewModel.agencies.push(new agency(a.Name, a.FullName));
        });
    }
});

服务接口定义如下:

 [ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        RequestFormat = WebMessageFormat.Json,
       ResponseFormat = WebMessageFormat.Json, UriTemplate = "getsupportedagencies")]
    AgencyDTO[] GetSupportedAgencies();

  // other methods
}

在跨域问题上遇到线程后,我手动将Global.asax文件添加到webservice项目中,还添加了以下方法方法:

 protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",
                         "http://localhost:80");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
                          "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
                          "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
                          "1728000");
            HttpContext.Current.Response.End();
        }

    }

我在web.config中的服务模型配置如下所示:

<system.serviceModel>       
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />     
        <services>
            <service behaviorConfiguration="ServiceBehavior" name="Services.Service1">
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <endpoint address="basic" binding="basicHttpBinding" contract="Services.Contracts.IService1" />
                <endpoint address="json" 
                    behaviorConfiguration="JsonBehavior"
                    binding="webHttpBinding" 
                    contract="Services.Contracts.IService1" />
            </service>   
        </services>
        <behaviors>
        <endpointBehaviors>
            <behavior name="JsonBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    <serviceBehaviors>
    <behavior name="ServiceBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
   </serviceBehaviors>
  </behaviors>      
    </system.serviceModel>

我使用以下代码对服务进行单元测试:

[Test]
 public void TestGetAgencyForLocation()
       {
           var client = new WebClient();

           var response = client.DownloadString(
                  "http://mydomain.com/service1/service.svc/json/getsupportedagencies");
           Assert.IsTrue(!string.IsNullOrEmpty(response));

           var agencies= JsonConvert.DeserializeObject(response);
           Assert.IsNotNull(agencies);
       }

如果我在Chrome或IE中发布网址“http://mydomain.com/service1/service.svc/json/getsupportedagencies”,我会以JSON格式获得正确的服务器响应。

是的,有了所有这些,我仍然得到405:方法不允许。

TIA。

1 个答案:

答案 0 :(得分:0)

我通过在WCF项目 Global.asax 文件中添加以下代码来解决此问题

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");

                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }