CORS problem when making GET call on Angular 6

0

When I call the following URL: link directly in the browser or in POSTMAN I get the JSON correctly , but when I try to call the same link via GET in the Angle, I get the error: "Failed to load link ...: Origin ' link > 'is therefore not allowed access.'

I'm using a normal HttpClient:

getTabela() {
  return this.http.get<TabelaEstatistica[]>('https://script.google.com/macros/s/AKfycbxk616n8wjgGeHZIc3Hm66Kcv4ZtWKZJQnEKLsxZC9LpoDK8mQZ/exec');
}

Does anyone know what I should do to receive JSON in my application?

    
asked by anonymous 11.09.2018 / 22:01

1 answer

0

I was able to figure out the problem. My Angular application has an interceptor class that takes all my HTTP requests and inserts a JWT, as I have a Spring application on the backend that expects this in all requests.

However, this GET request I'm making is not for my backend, but for an external webservice. When I send this JWT in the GET request to the google endpoint, they complain and give this CORS problem.

I would then have two solutions:

1) Instead of making this GET request straight from my Angular application, do the backend, treat the data and then give it to the angular.

2) Prevent that particular HTTP call from passing through my interceptor. It was the solution I used because it was faster for me, using HttpBackend, so the request will not go through the interceptor:

 private http: HttpClient;

  constructor(handler: HttpBackend) {
    this.http = new HttpClient(handler);
   }

  linkTabela = 'https://script.google.com/macros/s/AKfycbxk616n8wjgGeHZIc3Hm66Kcv4ZtWKZJQnEKLsxZC9LpoDK8mQZ/exec';

    getTabela() {
      return this.http.get<TabelaEstatistica>(this.linkTabela);
    }
    
12.09.2018 / 14:56