2017-01-16 42 views
0

我一直在努力CORS的問題,因爲一個星期。我正在使用web api2編寫Web服務。我創建了一個控制器(用於基本crud操作)並託管在服務器192.168.0.213:8041/api/user_creation中,並且我創建了另一個mvc5應用程序並託管在與http://192.168.0.213:8040/usercreation/Index相同的服務器中。我已經使用下面的鏈接啓用cors。Cors不工作刪除和放在網頁api2

http://www.c-sharpcorner.com/UploadFile/009ee3/implementation-of-cross-origin-request-in-Asp-Net-web-api-2/ 

我正在使用angularjs來獲取服務如下。我能夠從服務器獲取數據,並且能夠將數據推送到服務器。所以我的GET和POST動詞工作正常。每當我嘗試更新和刪除我面臨的問題,並得到以下錯誤消息。

Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://192.168.0.213:8041/User_Creation/1020 
XMLHttpRequest cannot load http://192.168.0.213:8041/User_Creation/1020. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.213:8040' is therefore not allowed access. The response had HTTP status code 405. 

我正在使用angularjs來調用Web服務,如下所示。 例如,對於更新,

this.update = function (sub) { 
     var url = 'http://192.168.0.213:8041/User_Creation/' + sub.user_id; 
     return $http({ 
      method: 'put', 
      data: JSON.stringify(sub), 
      url: url, 
      contentType: "application/json" 
     }); 
    } 

對於刪除,

this.deleteSubscriber = function (user_id) { 
     var url = 'http://192.168.0.213:8041/User_Creation/' + user_id; 
     return $http.delete(url).then(function (response) { 
      return response.data; 
     }); 
    } 

從我在JSON的形式接收數據網絡API所以我說下面的代碼在global.asx。 GlobalConfigGl​​obalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings

.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
     GlobalConfiguration.Configuration.Formatters 
      .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); 

這是在web.config中

<handlers> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 

下面我處理程序代碼的請求和響應數據。

Request URL: http://192.168.0.213:8041/User_Creation/1021 
    Request Method: DELETE 
    Status Code: 405/Method Not Allowed 
- Request Headers 
    Accept: application/json, text/plain, */* 
    Accept-Encoding: gzip, deflate 
    Accept-Language: en-US 
    Cache-Control: no-cache 
    Connection: Keep-Alive 
    Content-Length: 0 
    Host: 192.168.0.213:8041 
    Referer: http://192.168.0.213:8040/usercreation/Index 
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko 
- Response Headers 
    Allow: GET, HEAD, OPTIONS, TRACE 
    Content-Length: 1293 
    Content-Type: text/html 
    Date: Mon, 16 Jan 2017 08:34:30 GMT 
    Server: Microsoft-IIS/8.5 
    X-Powered-By: ASP.NET 

我面臨着PUT和Delete方法的問題。我可以幫忙嗎?有人能告訴我我在哪裏做錯了嗎?謝謝。

+0

在響應標題中檢查'Access-Control-Allow-Methods' – aseferov

+0

謝謝。當我點擊刪除時,我會得到一些細節。我張貼上面,但我不明白我失蹤。 –

+0

您能否顯示您的Web API控制器操作? –

回答

2

它看起來像PUTDELETE動詞不允許在您的網絡服務器上,您遇到的問題根本與CORS無關。有幾件事你可能想結賬,以確保首先允許這些動詞。在嘗試進行跨域AJAX調用之前,請確保您可以對您的Web API進行標準的HTTP調用,並且它會響應這些動詞。

所以在你的web.config,你可能有這樣的事情:

add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 

通知沒有在這裏PUTDELETE動詞。

還要確保您已刪除所有WebDAVWebDAVModule痕跡從你的web.config,因爲這可能會與你的要求會干擾:

<modules runAllManagedModulesForAllRequests="true"> 
    <remove name="WebDAVModule"/> 
</modules> 

現在你可以使用PUT成功調用您的Web API端點並使用Postman或Fiddler刪除動詞,您可能會回到JavaScript嘗試。

+0

謝謝,我將把我在處理程序中的內容加入 –

+0

您是否嘗試添加''line? –

+0

我剛剛改爲動詞=「GET,HEAD,POST,PUT,DELETE,DEBUG」,現在開始工作正常,發佈不工作,並刪除也。 –