CORS error when executing request using HttpClient in the Angular

1

I'm trying to perform an Http request using "HttpClient" from Angular 4, and I get the following error:

  

Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.

According to the research I did and with what I know of CORS would have to inform the Header of the request the origin, my doubt is. What is the correct way to perform this request obeying the rules of CORS using the Angular?

I tried the following code:

public get(url: string, params: HttpParams) {

    let options = {
        headers: new HttpHeaders()
            .set('Access-Control-Allow-Origin', 'http://localhost:4200'),
        params: params

    }

    return this.http.get<any>(url, options);
}

I am using the following url " link ", but when trying with other services the same error occurs.

I tried to replace ' link ' with '*' and did not succeed

    
asked by anonymous 08.02.2018 / 05:11

1 answer

2

Considering the documentation for the Climatempo API , after the token have been generated, you simply concatenate it to the end of the URL used for each HTTP request:

public get(url: string, params: HttpParams) {    
    return this.http.get<any>(url + "token=" + this.token, options);
}
    
11.02.2018 / 17:04