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

标签 angularjs azure asp.net-web-api microservices azure-service-fabric

我有一个 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/

相关文章:

select - select 中的 AngularJS ngModel 指令

python - 从服务器进行 Django REST 登录

azure - 使用 Azure Devops Pipeline 将数据复制到没有公共(public)网络访问权限的存储帐户

asp.net-mvc - MVC Web Api中的方法如何映射到http动词?

c# - ASP.NET WebAPI HttpGet 操作 - 查询字符串后模型为空

c# - ASP.NET Web API HTTP 400 错误请求

没有 sudo 访问权限的 AngularJS 和 PhantomJS

angularjs - Spring引导应用程序找不到index.html

azure - 证书存储访问被拒绝

node.js - 如何在 Azure 应用程序控制台上运行 npm 命令?