I am trying to fetch the json data from one URL which works if you paste on browser and also through postman but when I make GET requesting using Angular HttpClietn then getting CORS Policy error.
Can anyone help me to understand why this is not working.
URL : https://ant.aliceblueonline.com/api/v1/search?key=IDEA
Angular Code:
-- First Attempt
GetScripInfo(): Observable<any>{
let header = new HttpHeaders({ 'Accept': 'application/json' });
header.set('Content-Type', 'application/json');
const requestOptions = { headers: header};
return this.httpClient.get<any>("https://ant.aliceblueonline.com/api/v1/search?key=IDEA",
requestOptions).pipe(
map((res: any) => {
debugger;
res.json()})
);
}
-- Second Attempt
GetScripInfo(): Observable<any>{
let header = new HttpHeaders({ 'Access-Control-Allow-Origin': '*', });
header.set('access-control-allow-origin',"http://localhost:4200/");
header.set('withCredentials', "true");
header.set('Content-Type', 'application/json');
const requestOptions = { headers: header};
return this.httpClient.get<any>("https://ant.aliceblueonline.com/api/v1/search?key=IDEA", requestOptions);
}
Calling Method
GetScripInfo(value: string) {
debugger;
this.algoService.GetScripInfo().subscribe(result => {
debugger;
});
}
-- StackBlitz URL -- https://stackblitz.com/edit/angular-ivy-rbbwp6?file=src/app/app.component.ts