繁体   English   中英

在Azure Service Fabric Web Api中启用CORS

[英]Enabling CORS in Azure Service Fabric Web Api

我有一个有角度的应用程序,它将http请求发送到我的Service Fabric Web API(部署在Secure Service Fabric群集上),如下所示:

    $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);

当我在本地运行我的角度应用程序并尝试发送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.

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

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

即使我已经在Web API中全局启用了CORS,尤其是在安全群集中启用了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" />

CORS消息是红色鲱鱼。 如果查看错误消息的结尾,您将看到以下内容:

响应的HTTP状态码为500。

通常,响应将包含有关错误的一些详细信息。 我建议使用启用了HTTPS解密的Fiddler之类的工具,以便您可以看到响应的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM