【发布时间】:2015-05-26 20:59:14
【问题描述】:
我正在从 AngularJS 应用程序向 asp.net web api 发出删除请求,但出现以下错误 -
XMLHttpRequest cannot load http://api.prod.com/api/v1/proddetails/34. No
'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:5100' is therefore not allowed access. The response
had HTTP status code 403.
方法:
[HttpDelete]
public HttpResponseMessage Delete(int id)
网络配置:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
<remove name="WebDAVModule" />
</modules>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="12582912" />
<!--12 MB-->
</requestFiltering>
</security>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="WebDAV" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
请帮忙。
注意:该机器是在 AWS EC2 上运行 IIS 8.5 的 Windows Server 2012 R2。我在控制器上像这样正确设置了 CORS -
[EnableCors(origins: "*", headers: "*", methods: "*")]
编辑1:
我正在查看我的 applicationHost.config 文件,它有这些行 -
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="C:\windows\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" modules="IsapiModule" scriptProcessor="C:\windows\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" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
但是它们太多了,而且没有一个将 DELETE 作为允许的动词。这会导致问题吗?
编辑2:
根据要求,这是 AngularJS 请求代码 -
$http.delete('http://api.prod.com/api/v1/proddetails/' + data)
.success(function (data, status, headers, config) {
console.log(data);
if(data.status == "200")
{
console.log('data deleted');
}
deferred.resolve(data);
})
.error(function (data, status, headers, config) {
console.log("some error occurred");
deferred.reject(data);
});
【问题讨论】:
-
可能由于其他原因您收到 500 内部服务器错误,请尝试更好地调查服务器上发生的情况,有时“Access-Control-Allow-Origin”错误会产生误导
-
我在本地主机上尝试了相同的代码,它可以工作,但在生产中失败。 OPTIONS 请求为 200 OK,但 DELETE 请求为 403 Access Denied on production。
-
我认为它与此有关 - blogs.msdn.com/b/tmarq/archive/2010/05/26/…
-
差异可能是您本地环境中的模型与生产环境中的模型不同,因此,在生产环境中您没有删除记录的权限
-
请求甚至没有到达端点。我是本地和产品的管理员,并提供了所有访问权限。我在想,由于产品在 AWS EC2 上,可能存在一些防火墙问题。
标签: asp.net-mvc angularjs iis asp.net-web-api amazon-ec2