I am getting below error on call to REST Web API in Asp.net.
XMLHttpRequest cannot load http://localhost:54859/api/PostData. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
I am using Angular2 as Front end. In the back end, I have added following codes to enable CORS in WEB API.
var corsAttr = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsAttr);
Everything works fine for Http get request,but the same not for Http Post request.
Any help would be appreciable
Thanks in advance!
Adding
Access-Control-Allow-Origin
header for the preflight request during Application_BeginRequest in Global.asax.cs worked for me.Global.asax/Global.asax.cs
After solving this issue, the application threw errors on browser console that certain headers are not mentioned in preflight response.
Once the headers were added to
Access-Control-Allow-Headers
header of the preflight response it got resolved.This above code worked fine
I got it resolved by adding following lines to web.config.
Thanks.