使用 ASP.NET Web API 对 PUT 和 DELETE 的 CORS 支持 [英] CORS support for PUT and DELETE with ASP.NET Web API
问题描述
我正在使用 ASP.NET Web API 的最终版本来实现 JavaScript 友好的 API.根据各种教程,我在 web.config 中启用了 CORS:
I am working with the final version of ASP.NET Web API to implement a JavaScript-friendly API. Per various tutorials, I have enabled CORS in my web.config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
使用上述方法,跨域 GET 和 POST 请求工作正常,但 PUT 和 DELETE 请求均失败.
With the above, cross-domain GET and POST requests work fine, but PUT and DELETE requests both fail.
在 Chrome 中:
In Chrome:
Access-Control-Allow-Methods 不允许方法 PUT.
Method PUT is not allowed by Access-Control-Allow-Methods.
Access-Control-Allow-Methods 不允许方法 DELETE.
Method DELETE is not allowed by Access-Control-Allow-Methods.
是否需要额外的东西才能让 PUT 和 DELETE 动词跨域工作?
Is there something additional required to get PUT and DELETE verbs working cross-domain?
推荐答案
貌似加了一个自定义的header 就搞定了:
It looks like adding another custom header sorted it out:
<system.webServer>
<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>
</system.webServer>
这篇关于使用 ASP.NET Web API 对 PUT 和 DELETE 的 CORS 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!