0

I am using asp.net WebAPI together with Angular. When I make a call from Angular to a method on my WebAPI, I get an error in Chrome (It is working fine in IE), and I can see with Fiddler that the call is made as an "OPTIONS" (I am only using $http.post and $http.get), in IE it is made as a post or a get.

I have tried to config a header to send with the request ('Content-Type': 'text/plain'), and then it is working fine in Chrome. But the problem is that I am using Authorization that also is in the header, and when i add Authorization in the header, the hack with content-type isn't working anymore.

I have also tried to add the NuGet packages "Cors" to the WebAPI (and enabled it in the controller [EnableCors("", "", "*")]), but it has not made any changes.

Is there anyone that is having a solution for this problem?

Thanks very much in advance :-)

PNR
  • 555
  • 3
  • 12
  • 26

2 Answers2

0

Do you also add the code in Global.asax's Application_Start method when you use cors? Not just add EnableCorsAttributeon action

GlobalConfiguration.Configuration.EnableCors();

or you don't need to use cors lib,it is also used when you add children node in web.config's node system.webServer

<httpProtocol>    
  <customHeaders>    
    <add name="Access-Control-Allow-Origin" value="*" />    
    <add name="Access-Control-Allow-Headers" value="*" />    
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />    
  </customHeaders>    
</httpProtocol>
Sky Fang
  • 1,101
  • 6
  • 6
  • I tried that, but when I do that I get the following error: The 'Access-Control-Allow-Origin' header contains multiple values – PNR Jul 07 '15 at 13:21
  • I have also tried adding the parametes to web.config, but still the same problem :-/ But when adding 'Content-Type': 'text/plain' to the header it Works. But then there is the problem with the Authorization – PNR Jul 07 '15 at 13:52
  • about Authorization ,maybe you can find "Passing Credentials in Cross-Origin Requests" in http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api – Sky Fang Jul 07 '15 at 14:53
  • Thank you very much everybody for your comments. Finally it Works, I did not find the exact error, but is using CORS and after a lot of attempts, it finally worked  – PNR Jul 16 '15 at 10:25
0

You might want to add an httpInterceptor and add the Access-Control headers as describe in this answer of similar question

Community
  • 1
  • 1
nolazybits
  • 497
  • 4
  • 19