Reputation: 627
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:
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
Upvotes: 1
Views: 1795
Reputation: 86
Add this to your WebApiConfig
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
Upvotes: 2
Reputation: 669
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>
Upvotes: 3