ERRORS:
Failed to load resource: the server responded with a status of 403 ()login:1 Access to XMLHttpRequest at 'http://localhost:8080/login' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.core.js:7187 ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"http://localhost:8080/login","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost:8080/login: 0 Unknown Error","error":{"isTrusted":true}} at resolvePromise (zone-evergreen.js:797) at zone-evergreen.js:707 at SafeSubscriber._error (Observable.js:91) at SafeSubscriber.__tryOrUnsub (Subscriber.js:183) at SafeSubscriber.error (Subscriber.js:135) at Subscriber._error (Subscriber.js:75) at Subscriber.error (Subscriber.js:55) at MapSubscriber._error (Subscriber.js:75) at MapSubscriber.error (Subscriber.js:55) at FilterSubscriber._error (Subscriber.js:75)defaultErrorLogger @ core.js:7187
POST service in JAVA spring boot (URL of the microservice: http:localhost:8080/login):
@CrossOrigin(origins = "*")@RestControllerpublic class dmoController { @SuppressWarnings("rawtypes") @PostMapping("/login") public ResponseEntity<?> login(@RequestBody String data) { System.out.print("entra"); System.out.print(data); HttpHeaders headers = new HttpHeaders(); headers.add("Access-Control-Allow-Origin", "*"); headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); headers.add("Access-Control-Allow-Headers", "X-Requested-With,content-type"); headers.add("Access-Control-Allow-Credentials", "true"); return new ResponseEntity<>(headers, HttpStatus.CREATED); }}
Call to post from Angular (url angular: http:localhost:4200/login):
public async login(email: string, password: string) { const data = { username: 'pepa', password: 'manoli' }; httpOptions.headers.append('Access-Control-Allow-Origin', '*'); httpOptions.headers.append('Access-Control-Allow-Methods','GET, POST, OPTIONS, PUT, PATCH, DELETE', ); httpOptions.headers.append('Access-Control-Allow-Headers','X-Requested-With,content-type', ); httpOptions.headers.append('Access-Control-Allow-Credentials', 'true'); this.http.post(this.url, JSON.stringify(data), httpOptions).toPromise(); }