2015-06-14 42 views
0

我试图使用ASP.NET MVC Web API构建一个角度应用程序。Cors不解决跨源请求

当我做出$资源请求我的Web服务器(本地主机),我得到

的XMLHttpRequest无法加载http://localhost:59636/api/studios。请求的资源上没有“Access-Control-Allow-Origin”标题。因此'Origin'http://localhost:44060'不允许访问。答案有HTTP状态代码401.

我读过安装Cors会解决这个问题。但它没有。我曾经尝试都使CORS

config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 

以及使用该属性

[EnableCorsAttribute("*", "*", "*")] 

它正在研究IE

maybe this helps? I am calling the default api/values

回答

0

在你WebApiConfig.cs文件,你应该有:

 config.EnableCors(); 

在你ApplicationOAuthProvider.cs,在GrantResourceOwnerCredentials():

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", 
       new[] { "http://localhost:44060" }); 

,并通过控制器:

[EnableCorsAttribute("http://localhost:44060", "*", "*")] 
+0

仍然没有工作。我已注释掉 '//config.SuppressDefaultHostAuthentication(); //config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));' 并从控制器中删除授​​权属性 – den