我一直在努力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。 GlobalConfigGlobalConfiguration.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方法的問題。我可以幫忙嗎?有人能告訴我我在哪裏做錯了嗎?謝謝。
在響應標題中檢查'Access-Control-Allow-Methods' – aseferov
謝謝。當我點擊刪除時,我會得到一些細節。我張貼上面,但我不明白我失蹤。 –
您能否顯示您的Web API控制器操作? –