2012-04-09 164 views
1

即使要通過這個帖子 wcf REST Services and JQuery Ajax Post: Method not allowed (3)WCF REST服務返回405:方法不被允許的jQuery AJAX

我不能讓過去405後GET:試圖調用一個方法經由我的WCF REST服務時不得法下面的AJAX調用:

$.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文件的Web服務項目,還增加了以下方法方法:

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:方法不允許。

TIA。

+0

由於解決了這個問題,你解決了我的問題,這些線「HttpContext.Current.Response.AddHeader(」 Access-Control-Allow-Methods「, 」GET,POST「);」 – 2013-04-01 12:35:45

回答

1

我有完全相同的問題,除了我正在做純html項目,因此沒有地方追加頭,因爲我沒有c#。

我有人有這個答案,請張貼它......它一直讓我瘋狂!

感謝

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(); 
      } 
     } 
相關問題