2016-02-05 155 views
2

我有網絡API服務:

public void Delete(int id) 
     { 
} 

的Web API-的Web.config

<httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
     <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> 
     </customHeaders> 
    </httpProtocol> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <directoryBrowse enabled="true" /> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <handlers> 
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> 
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 

當我使用它使用郵差(Chrome擴展)。我將數據作爲「表單數據」傳遞。我得到200個返回碼。 我使用angularjs調用這個方法:

deletesurvey: function(surveyData) { 
       var objFormData = new FormData(); 
       for (var key in surveyData) 
        objFormData.append(key, surveyData[key]); 
       return $http.delete(configurationurl + 'Survey', objFormData, { 
        transformRequest: angular.identity, 
        headers: { 'Content-Type': undefined } 
       }); 

我已經設置頭在我的web.config:

<modules runAllManagedModulesForAllRequests="true" /> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,HEAD,DELETE,OPTIONS" /> 
     <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" /> 
     <add name="Access-Control-Request-Methods" value="*" /> 
     <add name="Access-Control-Max-Age" value="1728000" /> 

     </customHeaders> 
    </httpProtocol> 

它稱之爲deletesurvey功能它給我的錯誤後(Browser是鉻) xmlhttprequest無法加載響應URL的預檢具有無效http狀態代碼405

+1

錯誤消息爲預檢OPTIONS請求,而不是刪除請求,其瀏覽器永遠不會(因爲它沒有獲得對OPTIONS請求的響應中的許可) – Quentin

回答

0

似乎正在發生的是CORS「預檢」請求(它使用OPTIONS方法)沒有/不正確地由服務器處理。

推薦閱讀:

preflight請求的響應應該包含這樣的

OPTIONS/HTTP/1.1 
Origin: http://example.com 
Access-Control-Request-Method: DELETE 

Access-Control-Request-Method是主要的焦點在這裏)


至於爲何「Postman'展期,並不給這個老馬,它根本就不是受Same Origin Policy

+0

但是我應該如何設置我的應用程序。所以它的工作 –

+1

我對ASP.NET環境的知識有限(閱讀:不存在),我認爲你應該添加更相關的'asp.net'標籤。我唯一能做的就是提供一個[另一個問題]的鏈接(http://stackoverflow.com/questions/12521499/cors-support-for-put-and-delete-with-asp-net-web-api) (類似據我所能確定) –

+0

我已經添加了webapi web.config。 –

相關問題