I am writing a application in angular with java. Here I am writing a get method in angular, it is going to front end and the back end java service is returning a list of object. But the angular get method is directly going to error part.
Java service:-
@GetMapping(value = "/getStatusMails")
public ResponseEntity<Object> getStatusMails() {
List<Dto> status= sercice.getStatusMails(123);
return new ResponseEntity<Object>(status, HttpStatus.OK);
}
angular get method: -
this.httpClient.get<any>('http://IP:8090/getStatusMails').subscribe(
data => this.elements = data,
error => console.error('There was an error!', error))
I have tested with postman, it is getting the proper list of objects.
My console error: -
Access to XMLHttpRequest at 'http://IP:8090/getStatusMails' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
zone-evergreen.js:2845 GET http://IP:8090/getStatusMails net::ERR_FAILED
core.js:6241 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://IP:8090/getStatusMails", ok: false, …}
If I write my code like below
const url = "http://IP:8090/getStatusMails"; // site that doesn’t send Access-Control-*
fetch(url)
.then(response => response.text())
.then(contents => console.log(contents))
.catch(() => console.log("Can’t access " + url + " response. Blocked by browser?"))
}
In my console I am getting the console like Can’t access http://IP:8090/getStatusMails response. Blocked by browser?
When I click on my url in the log, I am able to see the output in another window.