1
I am 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.
According to the researches I did and what I know of CORS would have to inform in 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 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’m using the following url "http://apiadvisor.climatempo.com.br/api/v1/forecast/locale/3477/hours/72?token=XXXXX", but when trying with other services the same error occurs.
I tried to replace 'http://localhost:4200' by '*' and was unsuccessful
your server allows cross-origin requests?
– mercador
The server I am trying to access allows yes, it is the Weather Forecast api of Weather, but I am making the call at the beginning of a local server, I just lifted a local server with "ng serve", I need to make some local configuration tbm?
– Horrander Maikel
from what I understand by looking at the documentation of this api you should generate a token and enable Cors there. In your angular application you should then attach this token generated
– mercador
First thank you for your commitment to help and I have already done so, "http://apiadvisor.climatempo.com.br/api/v1/forecast/locale/3477/hours/72?token=XXXXXX", I just switched the token to XXXXX, the problem is that I tried to consult other services and had the same error, so I’m thinking I set up the requisition headers wrong
– Horrander Maikel
You mean the 72-hour forecast per city ID worked but no other requisition worked? Can you tell me what other requests you tried?
– mercador
Because in my view all the requests available in this api are get requests, where the parameters are passed by the url. That way, you just need to concatenate
&token=your-app-token
in each of thehttp.get
for this api– mercador
No request worked, neither in this weather service, nor in another service, as the Open Weather API, seems to be something very simple even, I concatenei the token and did a get, but appears Cors error, even if I set in the request header, Do you know if there is another way to set up the header? Otherwise: ". set('Access-Control-Allow-Origin', 'http://localhost:4200')"
– Horrander Maikel
Let’s go continue this discussion in chat.
– mercador