Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在Azure服务结构Web Api中启用CORS_Angularjs_Azure_Asp.net Web Api_Microservices_Azure Service Fabric - Fatal编程技术网

Angularjs 在Azure服务结构Web Api中启用CORS

Angularjs 在Azure服务结构Web Api中启用CORS,angularjs,azure,asp.net-web-api,microservices,azure-service-fabric,Angularjs,Azure,Asp.net Web Api,Microservices,Azure Service Fabric,我有一个angular应用程序,它向我的Service Fabric Web API(部署在安全的Service Fabric群集上)发送http请求,如下所示: 我还在web api启动类中全局启用了CORS,如下所示: HttpConfiguration config = new HttpConfiguration(); var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); 当我在本地运行an

我有一个angular应用程序,它向我的Service Fabric Web API(部署在安全的Service Fabric群集上)发送http请求,如下所示:

我还在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直接从浏览器访问我的服务

此外,当我尝试在一个不安全的服务结构集群上部署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


通常,响应将包括有关错误的一些细节。我建议使用像Fiddler这样启用HTTPS解密的工具,这样您就可以看到响应的内容。

请注意,您不需要在角度侧设置CORS标头。由于响应状态为500,您的API出现问题。是的,我就是这么想的,我也这么想。你能发布整个Web API配置,而不仅仅是注册CORS的行吗?但这是除了路由部分之外添加到默认配置行的唯一部分。我能够通过使用nodejs代理服务器来路由我的所有流量来解决这个问题。但仍然无法在服务器端修复它。我尝试添加此。仍然收到相同的错误。即使没有这行代码,我也能够调用部署在我的不安全集群上的Api。您是否尝试从客户端的请求中删除头文件?
<package id="Microsoft.AspNet.Cors" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net45" />