gpt4 book ai didi

c# - 'Access-Control-Allow-Origin' header 包含多个值。但是我在标题中只定义了一个值?

转载 作者:行者123 更新时间:2023-12-02 01:22:55 26 4
gpt4 key购买 nike

提出请求时,我会抛出以下错误:

Access to XMLHttpRequest at 'My Server URL' from origin 'Server Name' has been 
blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple
values 'Server Name, *', but only one is allowed.

但是在我的 WebApiConfig.cs 文件中,我定义了 CORS 策略如下:
var cors = new EnableCorsAttribute("MyServerName", "Content-Type", "GET,PUT,POST,DELETE");
config.EnableCors(cors);

我只定义了一个值 MyServerName,但抛出的错误将其定义为 'Server Name, *'
更新:

在我的 WebApiConfig.cs 文件中禁用我的 CORS 定义时,我在发出请求时收到以下错误:
Access to XMLHttpRequest at 'My Server URL' from origin 'Server Name' has been 
blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in
the response must not be the wildcard '*' when the request's credentials mode is
'include'. The credentials mode of requests initiated by the XMLHttpRequest is
controlled by the withCredentials attribute.

我的 Web.config 文件中没有定义 CORS。

更新 2

我的 Access-Control-Allow-Origin 值是在我的 IIS 中定义的,在更改并运行它后,它认为该值是 ''。

最佳答案

总结自 The 'Access-Control-Allow-Origin' header contains multiple values有多种添加 CORS 的方法,您可能有多种方法:

  • 你这样做的方式。
  • 调用 app.UseCors(CorsOptions.AllowAll);
  • 通过在 web.config 中添加它
  • <system.webServer>
    <httpProtocol>
    <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
    </httpProtocol>
  • 并通过使用 Cors 属性。
  • Web 服务器上的 IIS 或其他配置。重新启动应用程序池,它应该可以工作。
  • 关于c# - 'Access-Control-Allow-Origin' header 包含多个值。但是我在标题中只定义了一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668753/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com