【发布时间】:2017-04-22 11:58:43
【问题描述】:
我有一个 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);
});
我还在我的 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 时,相同的 http 请求也有效,并且在启动类中添加了相同的行以启用 CORS。
为什么即使我已经在我的 Web API 中全局启用了 CORS,尤其是当它在安全集群上时,还是会发生这种情况?
【问题讨论】:
-
请注意,您不需要在 Angular 端设置 CORS 标头。由于响应状态为 500,因此您的 API 出现问题。
-
是的,我也是这么想的..
-
您能否发布整个 Web API 配置,而不仅仅是注册 CORS 的行?
-
但这是添加到默认配置行中的唯一部分,而不是路由部分
-
我能够通过使用 nodejs 代理服务器来路由我的所有流量来解决这个问题。但仍然无法在服务器端修复它。
标签: angularjs azure asp.net-web-api microservices azure-service-fabric