为什么在为所有来源添加CORS后出现错误

问题描述 投票:-1回答:1

[当我尝试从客户端访问WEB API时,出现CORS错误。

为此,我在IIS中添加CORS Access-Control-Allow-Origin:*:

enter image description here

但是在添加Access-Control-Allow-Origin:*之后,我在客户端收到此错误:

localhost/:1 Access to XMLHttpRequest at 'http://localhost/fridge/api/user/15' from origin 'http://localhost:4200' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:4200, http://localhost:4200', but only one is allowed.

任何想法如何解决以上错误?为什么会出现此错误?

iis cors
1个回答
0
投票

好像您添加了两个标题条目。

您可以尝试删除WebApiConfig.cs或web.config cors条目。

另一种方法是,您可以尝试添加确切的域而不是*。

var CorsAttribute = new EnableCorsAttribute("http://www.example.com","", ""); config.EnableCors(CorsAttribute);

如果您使用webservice(.asmx)作为API

[从Global.asax.cs文件的begin_request函数或web.config中删除'Access-Control-Allow-Origin'详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.