gpt4 book ai didi

c# - URL 已被 CORS 策略阻止

转载 作者:行者123 更新时间:2023-12-05 01:32:03 24 4
gpt4 key购买 nike

我在 Angular 版本 1 中从 ASP.Net MVC 调用 API 时遇到以下错误

enter image description here错误:CORS 策略已阻止从源“URL 2”访问“URL 1”处的 XMLHttpRequest:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态。

我在 WebApiConfig 中使用以下代码:

     var corsAttr = new EnableCorsAttribute("*","*","*");
config.EnableCors(corsAttr);

之后我尝试使用下面的代码:

var corsAttr = new EnableCorsAttribute("*","Content-Type", "GET,POST,PUT,DELETE,OPTIONS");
config.EnableCors(corsAttr);

和:

var corsAttr = new EnableCorsAttribute("https://rushpeik.ir","Content-Type", "GET,POST,PUT,DELETE,OPTIONS");
config.EnableCors(corsAttr);

但问题依旧

最佳答案

我找到了解决方案:

首先必须在WebApiConfig中使用下面的代码

     var corsAttr = new EnableCorsAttribute("*","*","*");
config.EnableCors(corsAttr);

然后将下面的代码添加到 web.config

<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" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>

我们必须允许“Access-Control-Allow-Headers”中的所有自定义 header ,如下行所示:

 <add name="Access-Control-Allow-Headers" value="Content-Type,Token,Username" />

它对我有用:)

关于c# - URL 已被 CORS 策略阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65808549/

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