[英]Call from Angular to webAPI on different domain is turned into OPTIONS
I am using asp.net WebAPI together with Angular. 我将Angular与asp.net WebAPI一起使用。 When I make a call from Angular to a method on my WebAPI, I get an error in Chrome (It is working fine in IE), and I can see with Fiddler that the call is made as an "OPTIONS" (I am only using $http.post and $http.get), in IE it is made as a post or a get. 当我从Angular调用WebAPI上的方法时,我在Chrome中收到一个错误(在IE中工作正常),并且可以通过Fiddler看到该调用是作为“ OPTIONS”进行的(我仅使用$ http.post和$ http.get),在IE中,它是作为帖子或获取的。
I have tried to config a header to send with the request ('Content-Type': 'text/plain'), and then it is working fine in Chrome. 我尝试配置标头与请求一起发送(“ Content-Type”:“ text / plain”),然后在Chrome中工作正常。 But the problem is that I am using Authorization that also is in the header, and when i add Authorization in the header, the hack with content-type isn't working anymore. 但是问题是我正在标题中使用授权,并且当我在标题中添加授权时,具有内容类型的hack不再起作用。
I have also tried to add the NuGet packages "Cors" to the WebAPI (and enabled it in the controller [EnableCors(" ", " ", "*")]), but it has not made any changes. 我还尝试将NuGet包“ Cors”添加到WebAPI(并在控制器[EnableCors(“ ”,“ ”,“ *”)]中启用了它),但未进行任何更改。
Is there anyone that is having a solution for this problem? 有没有人可以解决这个问题?
Thanks very much in advance :-) 首先十分感谢 :-)
Do you also add the code in Global.asax's Application_Start method when you use cors? 使用cors时,是否还会在Global.asax的Application_Start方法中添加代码? Not just add EnableCorsAttribute
on action 不仅在操作中添加EnableCorsAttribute
GlobalConfiguration.Configuration.EnableCors();
or you don't need to use cors lib,it is also used when you add children node in web.config
's node system.webServer
或不需要使用cors lib,在web.config
的节点system.webServer
添加子节点时也会使用它
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />
</customHeaders>
</httpProtocol>
您可能想要添加httpInterceptor并添加访问控制标头,如在类似问题的此答案中所述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.