Using AngularJS hosted on an ASP.NET server.
Making a call to a web API.
When making a POST request to the web API without parameters, it works. However, when passing parameters to the POST request, it returns the following error:
Error:
XMLHttpRequest
cannot load http://localhost:60117/api/Parts
. The response to the preflight request does not pass the access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://localhost:64658
is not allowed access.
Note: CORS has already been enabled through NuGet. [EnableCors("*", "*", "*")]
is included in the webapiconfig.cs
file.
var cors = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
The following is my request header:
OPTIONS /api/Parts HTTP/1.1
Host: localhost:60117
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:64658
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:64658/app/views/Index.html
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
When adding the following code to the web.config file, it results in a multiple accept verb error:
<httpProtocol>
<customHeaders>
<clear/>
<add name="Access-Control-Expose-Headers " value="WWW-Authenticate"/>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, PATCH, DELETE"/>
<add name="Access-Control-Allow-Headers" value="accept, authorization, Content-Type"/>
<remove name="X-Powered-By"/>
</customHeaders>
</httpProtocol>
Updated:
Now receiving the following message in response to the request:
The requested resource does not support the HTTP method 'POST'.
AJAX call:
$http({
method: "POST",
url: config.APIURL + 'Parts',
data: {'final':'final'}
});
Any assistance with this issue would be greatly appreciated. I am currently stuck and unable to progress.