Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular -> Web Api 2: "No 'Access-Control-Allow-Origin' header"

Basically I'm trying to access my web api which is uploaded on Azure.

I've added my CORS attribute to my WebApiConfig class:

public static void Register(HttpConfiguration config)
{
    var cors = new EnableCorsAttribute("http://localhost:51407", "*", "*");
    config.EnableCors(cors);

However whenever I send an Ajax request (using angularJS) which rightfully contains

Origin: http://localhost:51407

I get the error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:51407' is therefore not allowed access.

What could be the issue?

like image 497
JensOlsen112 Avatar asked Oct 20 '14 20:10

JensOlsen112


1 Answers

To fix this issue, you need to configure CORS-es. First go to "Web.config", find "system.webServer" tag, inside of it add next lines:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>

That is all.

like image 71
nikola.maksimovic Avatar answered Oct 20 '22 19:10

nikola.maksimovic