我正在使用ASP.NET Web API的最终版本来实现一个JavaScript友好的API。根据各种教程,我已经启用CORS在我的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请求都失败。
在Chrome中:
Method PUT is not allowed by Access-Control-Allow-Methods.
Method DELETE is not allowed by Access-Control-Allow-Methods.
是否需要额外的东西来获得PUT和DELETE动词跨域?
解决方法
它看起来像添加另一个自定义标题排序:
<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>