I'm try to build an app using Angular 5 through Webstorm. I have set up my services at back end. When i am trying to retrieve data from postman or from Rest client tool that Webstorm provide everything is ok.
But when i try to get data from my actual code of typescript angular says
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:4200' is therefore not allowed access.
I have add headers at the backend on my java service such as
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
response.addHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
My fuction at export class is below
constructor(private http: HttpClient) { }
login(username: string, password: string) {
console.log(username + '' + password);
const headers = new HttpHeaders().append('Content-Type', 'application/json')
const url = 'http://localhost:8080/../UserLogin';
return this.http.post(url, {'username': username, 'password': password} )
.subscribe(
(data: any) => {
console.log(data);
}
);
}
When i check the request Headers from Network Session of chrome i see that the request method is options and there is nowhere the request data.
I am kind confused about why postman and the rest client of webstrom run nice instead of my function and the second one about the request method which convert from Post to OPTIONS
Any help would be appreciate.