I am working on an Angular(v13) project that it's backend is Java and web servers are Tomcat and Nginx. My project is on localhost but the APIs are on another address(DNS). When I run the ng serve
command, I see this error on the browser console panel:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://X.com/club/v1/vPersonLevelService/vPersonLevelDashboard. (Reason: header ‘access-control-allow-headers’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response).
My service.ts code is here:
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { APP_CONFIG, IAppConfig } from '../club/core/app.config';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class NewsService {
constructor(@Inject(APP_CONFIG) private appConfig: IAppConfig, private http: HttpClient) { }
GetPersonLevel(){
let header = new HttpHeaders()
const corsHeaders = {
'observe': 'response',
'Access-Control-Allow-Headers': 'Content-Type,
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
'Access-Control-Allow-Origin': '*'
}
return this.http.get('https://X.com/club/v1/vPersonLevelService/vPersonLevelDashboard',{ headers: corsHeaders}).pipe( map( (res: any) => { return res; }) );
}
}
Can anyone help me to solve this error?