I am trying to call foreman API from other different domain. But always getting cors origin error. Using ajax and angular but both send the same error.
Actual error:
Access to XMLHttpRequest at ‘https://192.168.x.xxx/api/v2/config_reports/1914’ 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.
Angular code:
public getReport(){
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT',
'Authorization': 'Basic ' + btoa('admin:Test123@')
})
};
return this.httpClient.get<object[]>('https://192.168.8.137/api/v2/config_reports/1914', httpOptions);
}
Finally, I found solution. So I have to set cors with foreman-installer
.
foreman-installer --foreman-cors-domains http://localhost:4200 /* Your domain name*/