Cors doesnt solve cross origin requests

101 views Asked by At

I have trying to build an angular app using ASP.NET MVC Web API.

When i make a $resource request to my web server (localhost) I get

XMLHttpRequest cannot load http://localhost:59636/api/studios. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:44060' is therefore not allowed access. The response had HTTP status code 401.

I read that installing Cors would solve this. But it doesnt. I have tried both enabling cors

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

as well as using an the attribute

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

It is working on IE

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

1

There are 1 answers

1
obreja catalin On

In your WebApiConfig.cs file you should have:

        config.EnableCors();

In your ApplicationOAuthProvider.cs, in GrantResourceOwnerCredentials():

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

And over the controller:

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