2012-08-06 51 views
2

我最近刷了我的jQuery,當我過去發佈後,我來到一個問題,我無法得到一個wcf服務的響應,我經常得到405 - 方法不允許。我的要求對我來說很好,我想知道我是否錯過了至關重要的事情,但是爲什麼會發生這種情況。jQuery發佈到WCF服務返回405錯誤

這裏是後置碼被使用:

$.ajax({ 
    type: "POST", 
    url: "http://localhost:59929/CustomerService/GetCustomers", 
    data: null, 
    ContentType: "application/json", 
    dataType: "json", 
    success: function (msg) { 
     alert("Called and got: " + msg); 
    }, 
    error: function (result) { 
     alert('Service call failed: ' + result.status + '' + result.statusText); 
    } 
}); 

WCF代碼如下:

[ServiceContract] 
public interface ICustomerService 
{ 

    [OperationContract] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 
    List<Customer> GetCustomers(); 

    [OperationContract] 
    OperationStatus InsertCustomer(Customer cust); 
} 

與配置爲如下:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

的Fiddler示出了原發布爲:

POST http://localhost:59929/CustomerService/GetCustomers HTTP/1.1 
Host: localhost:59929 
Connection: keep-alive 
Content-Length: 0 
Origin: http://localhost:59513 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.60 Safari/537.1 
Accept: application/json, text/javascript, */*; q=0.01 
Referer: http://localhost:59513/LearnJQuery2/ajax/ajax_post.htm 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 

和提琴手也證實了405響應。

+1

你在哪裏定義了你的配置中的端點元素?此外,請確保使用正確的行爲元素公開webHttpBinding – Rajesh 2012-08-06 15:43:34

回答

1

嗯,我看了很久,然後意識到這沒有什麼不對,但pleathora :)所以繼承人的問題解決了。

所有他們必須確實來自同一個域和相同的端口和協議的拳頭,事實並非如此。我將我的服務移到了我的應用程序中,並適當地配置了綁定。接下來的部分是正確裝飾你的WCF服務,所以這裏是正確配置服務的代碼。

ICustomerService.cs

[ServiceContract] 
public interface ICustomerService 
{ 
    [OperationContract] 
    [WebInvoke(
     Method = "POST" , 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     ResponseFormat = WebMessageFormat.Json)] 
    List<JSONCustomer> GetCustomers(); 
} 

CustomerService.cs

[AspNetCompatibilityRequirements(RequirementsMode 
    = AspNetCompatibilityRequirementsMode.Allowed)] 
public class CustomerService : ICustomerService 
{ 
    public List<JSONCustomer> GetCustomers() 
    { 
     return new List<JSONCustomer> 
     { 
      new JSONCustomer {id = 1, FirstName = "john", LastName = "Doe"}, 
      new JSONCustomer {id = 2, FirstName = "jane", LastName = "Doe"},   
     }; 
    } 
} 

的Web.config

<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="EndpBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="ServiceBehavior" 
       name="CustomerService"> 
     <endpoint address="" 
        binding="webHttpBinding" 
        contract="ICustomerService" 
        behaviorConfiguration="EndpBehavior"/> 
     </service> 
    </services> 

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

</configuration> 

下一頁這裏正在使用Ajax代碼(整個網頁):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Ajax Post</title> 
     <script type="text/javascript" src="../scripts/jquery-1.7.2.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $('#HelpButton').click(function() { 
        $.post('../CustomerService.svc/GetCustomers', null, 
         function (data) { 
          var custs = data["GetCustomersResult"]; 
          var text = ''; 

          $(custs).each(function() { 
           text += '<span>' + this.FirstName + ' ' + this.LastName + '</span><br/>'; 
          }); 

          $('#OutputDiv').html(text); 
         } 
        , 'json'); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <input id="HelpButton" type="button" value="Press me"/> 
     <div id="OutputDiv" /> 
    </body> 
</html> 

JSONCustomer.cs

[DataContract] 
public class JSONCustomer 
{ 
    [DataMember] 
    public int id { get; set; } 

    [DataMember] 
    public string FirstName { get; set; } 

    [DataMember] 
    public string LastName { get; set; } 
} 

,我是真的希望那些現在有問題會發現這方面的幫助,這一點很重要,你要注意所有的事情,綁定,裝飾品和Ajax代碼jQuery的一個小滑,它不會工作。

1

你打電話給哪個域名?這可能是由Same origin policy造成的。如果是這種情況,請嘗試使用JSONP而不是JSON。

另外,你有沒有嘗試像Fiddler這樣的工具來查看請求/響應到底是什麼?這可以說明發生了什麼。

+0

檢查https://開發者。mozilla.org/en-US/docs/Same_origin_policy_for_JavaScript,如果主機,端口或協議不同,相同的來源將失敗。來自fiddler跟蹤的端口號看起來不同。試試JSON-P? – Fermin 2012-08-06 14:59:06

+0

這部分是答案,但唉,並非整個事情,我因此贊成但不接受這個作爲我在下面提供的主要答案,感謝您的意見。 – 2012-08-06 19:55:32

相關問題