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