1

即使通过这篇文章 wcf REST Services 和 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
}

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

 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 中发布 URL“ http://mydomain.com/service1/service.svc/json/getsupportedagencies ”,我会得到 JSON 格式的正确服务器响应。

是的,所有这些都到位后,我仍然得到 405: method not allowed。

TIA。

4

1 回答 1

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();
            }
        }
于 2017-05-29T05:33:26.663 回答