gpt4 book ai didi

javascript - 为 MVC 和 AngularJS 启用 CORS

转载 作者:行者123 更新时间:2023-11-30 08:31:01 26 4
gpt4 key购买 nike

我有一个 IONIC/AngularJS 应用程序,我必须从中调用我的 ASP.NET MVC Controller 方法,即 Login。它适用于 $http.get() 但当我使用 $http.post()$http({method: 'POST'}) 它返回一个错误。错误是(对于 Chrome):

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

我已经按照这个问题的答案为服务器启用了 CORS:

CORS in ASP .NET MVC5

我还包含了 nuget 包来启用 CORS,但这并没有解决我的问题。我还尝试在 Angular 的 POST 请求中添加以下内容:

$http({
method: 'POST',
url: url,
headers: {'Access-Control-Allow-Origin': '*'},
data: _data}).then();

但是也没用。如果我遗漏了什么,请告诉我。

谢谢

最佳答案

将此添加到您的 web.config 文件

<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>

关于javascript - 为 MVC 和 AngularJS 启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37991467/

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