gpt4 book ai didi

angularjs - 在 Azure Service Fabric Web Api 中启用 CORS

转载 作者:行者123 更新时间:2023-12-02 06:33:07 26 4
gpt4 key购买 nike

我有一个 Angular 应用程序,它向我的 Service Fabric Web API(部署在安全 Service Fabric 集群上)发送 http 请求,如下所示:

    $scope.request_headers = {
"Content-Type": "application/xml; charset=utf-8",
"Access-Control-Allow-Origin":"*"
}
$http({
url: "Service_Fabric_web_api_url",
method: "GET",
headers:$scope.request_headers
}).
then(function (result) {
console.log(result);
});

我还在我的 Web api 启动类中全局启用了 CORS,如下所示:

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

当我在本地运行我的 Angular 应用程序并尝试发送 http 请求时,我仍然收到此错误:

XMLHttpRequest cannot load Service_Fabric_web_api_url. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:xxxxx' is therefore not allowed access. The response had HTTP status code 500.

我可以使用相同的 URL 直接从浏览器访问我的服务。

此外,当我尝试在不安全的 Service Fabric 群集上部署 Web Api 并将相同的行添加到启动类以启用 CORS 时,相同的 http 请求也有效。

即使我在 Web API 中全局启用了 CORS,尤其是在安全集群上时,为什么还会发生这种情况?

最佳答案

在您的 Startup.cs 类中,有这一行吗? :

 public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
}

还有几个与 Cors 相关的 NuGet 包:

<package id="Microsoft.AspNet.Cors" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net45" />

关于angularjs - 在 Azure Service Fabric Web Api 中启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019199/

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