Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,

I have created one basic web api to interact with the database.
I am calling this api using Angular application, due to CORS error i have added below line of code in Global.asax file.

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","http://localhost:4200");

Now i can fetch my employee details using api however while adding new employee details again i am facing CORS error, as mentioned below.

Access to XMLHttpRequest at 'http://localhost:59571/api/Employee' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I am new to both API and Angular 2 technologies, so please help me with solution.

What I have tried:

i have added below line of code in Global.asax file.

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","http://localhost:4200");
Posted
Comments
Sachin11089 25-Jun-19 4:53am    
Hi,

I have searched in google and found below solution.I have added below line in global.asax file of Web api project.

if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Content-Type, Accept");
HttpContext.Current.Response.End();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900