1
votes

I have an IONIC/AngularJS application from which I have to call my ASP.NET MVC controller method i.e Login. It works with fine with $http.get() but when I use $http.post() or $http({method: 'POST'}) it returns an error. The error is (for Chrome):

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

I have enabled CORS for Server by following the answer to this question:

CORS in ASP .NET MVC5

I also included nuget package to enable CORS, but this didn't solve my problem. I also tried adding the following in Angular's POST request:

$http({
     method: 'POST',
     url: url,
     headers: {'Access-Control-Allow-Origin': '*'},
     data: _data}).then();

but it didn't work either. Please let me know if I am missing something.

Thankx

2

2 Answers

3
votes

Add this to your web.config file

<system.webServer>
  <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>
</system.webServer>
2
votes

Add this to your WebApiConfig

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);