Error SEC7120: [CORS] The origin 'http://localhost:4200' did not find 'http://localhost:4200' in the Access-Control-Allow-Origin response

12.6k views Asked by At

Actually I am trying to connect an Angular app with my Maven-Jersey-webapp(implements Rest Services concept) and getting this error.

More Details: I am using Oracle10g XE Database Eclipse IDE for maven Project(http://localhost:8080/MavenProject) Angular CLI(http://localhost:4200/)

I want to retrieve data from database(Stored in students table) and then display that data in Angular App in a Table my jersey project returns the table data in JSON and XML format(Running Successfully)

retrieving student table data in JSON format using Maven-jersey-webapp implementing rest

angular code making get request using HttpClient

error which i get while making get request

3

There are 3 answers

0
Israel Ocbina On

You can fix this by checking if you are using

http vs https

Just use http to fix it. After that, Add that in the header in the angular service you created like doing a code like this

 const httpOptions = {
            headers: new HttpHeaders({
                'Access-Control-Allow-Origin': 'http://localhost:4200', // -->Add this line
                'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
                'Access-Control-Allow-Headers': '*',
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            })
        };

Just check the string value you passed in your code. Sometimes your eye gets trick on these values. Like

http://localhost:4200 vs https://localhost:4200

0
Michael Winther On

I had the exact same error.

The issue was in the .net core web api project. The method I made a post to, had a object defined in the parameter list, without an empty constructor.

The CORS error was really misleading.

0
Bipin kumar On

Open the file App_Start/WebApiConfig.cs. Add config.EnableCors(); in WebApiConfig.Register method.