2012-12-21 57 views
1

我想在asp.net中爲我的應用程序編寫後端,但無法讓我的WCF服務在從Ext發出Ajax請求後返回POST響應。相反,我得到405'方法不允許'的錯誤。405來自.NET的響應錯誤WCF服務

這是我的要求:

Ext.Ajax.request({ 
    type: 'POST', 
    url: 'http://localhost:35798/RestServiceImpl.svc/export', 
    params: { 
     html  : {'array': document.body.innerHTML} 
    }, 
    success : function(response){ 
    console.log('RESPONSE !', response); 
     //me.onSuccess(response, callback, errback); 
    }, 
    failure : function(response){ 
     console.log('RESPONSE FAIL !', response); 
    } 
}); 

這是我的接口:

namespace RestService 
{ 
    public class RestServiceImpl : IRestServiceImpl 
    { 
     #region IRestServiceImpl Members 

     public string JSONData() 
     { 
      return "Your POST request"; 
     } 

     #endregion 
    } 
} 

和服務代碼:

using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Web.Script.Services; 

namespace RestService 
{ 

    [ServiceContract] 
    public interface IRestServiceImpl 
    { 
     [OperationContract] 
     [ScriptMethod] 
     [WebInvoke(Method = "POST", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Bare, 
      UriTemplate = "export")] 
     string JSONData(); 
    } 
} 

最後的配置:

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

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour"> 
     <!-- Service Endpoints --> 
     <!-- Unless fully qualified, address is relative to base address supplied above --> 
     <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web"> 
      <!-- 
       Upon deployment, the following identity element should be removed or replaced to reflect the 
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
       automatically. 
      --> 
     </endpoint> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehaviour"> 
      <!-- 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="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 
+2

第一個猜測:我會說你需要擺脫ajax請求中的「params」,因爲這個方法不需要。 – Pete

回答

1

這可能是一個跨域問題。例如,如果您的服務位於http://localhost:35798,並且發出AJAX請求的網頁位於http://localhost:80,那麼瀏覽器仍然會將其視爲不同域(即使只有端口號不同)。在這種情況下,一些瀏覽器發出CORS請求,如果你的服務沒有處理它,將返回一個405.你的選擇是(1)避免跨域請求,(2)實現CORS(不會幫助瀏覽器不支持它),或者(3)使用JSONP。