在 Azure Service Fabric Web 中启用 CORS Api
Enabling CORS in Azure Service Fabric Web Api
我有一个 angular 应用程序向我的 Service Fabric Web API(部署在 Secure 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);
});
我还在我的网络 api 启动 class 中全局启用了 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 并将相同的行添加到启动 class 以启用 CORS 时,相同的 http 请求也有效。
即使我在我的 Web API 中全局启用了 CORS,尤其是在安全集群上时,为什么还会发生这种情况?
在你的Startup.cs
class中,你有这行吗? :
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 消息是一个转移注意力的信息。如果您查看错误消息的末尾,您会看到:
The response had HTTP status code 500.
通常响应会包含有关错误的一些详细信息。我建议使用启用 HTTPS 解密的 Fiddler 之类的工具,这样您就可以看到响应的内容。
我有一个 angular 应用程序向我的 Service Fabric Web API(部署在 Secure 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);
});
我还在我的网络 api 启动 class 中全局启用了 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 并将相同的行添加到启动 class 以启用 CORS 时,相同的 http 请求也有效。
即使我在我的 Web API 中全局启用了 CORS,尤其是在安全集群上时,为什么还会发生这种情况?
在你的Startup.cs
class中,你有这行吗? :
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 消息是一个转移注意力的信息。如果您查看错误消息的末尾,您会看到:
The response had HTTP status code 500.
通常响应会包含有关错误的一些详细信息。我建议使用启用 HTTPS 解密的 Fiddler 之类的工具,这样您就可以看到响应的内容。