0

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

vijay sahu
  • 765
  • 1
  • 7
  • 29
  • There might be something wrong with yor headers and requestOptions. "Accept" and "Content-Type" is "application/json" by default so you could try your `get` without options. Also, `res.json` should not be needed as it parses the response data by default since a few Angular versions. – sandrooco Dec 08 '21 at 12:45
  • Does this answer your question? [How to add CORS request in header in Angular 5](https://stackoverflow.com/questions/47345282/how-to-add-cors-request-in-header-in-angular-5) – Batajus Dec 08 '21 at 13:06
  • any other suggestions ? – vijay sahu Dec 15 '21 at 11:48

0 Answers0