Weather API request cors error

ε祈祈猫儿з 提交于 2020-05-09 05:19:15

问题


I am trying to get weather data from dark sky api and I keep getting a cors error. Here is my code:

var url = `https://api.darksky.net/forecast/febb2871126cd24613f32a79c32d4158/${lat},${lon}`;
axios.get(url, config).then(response => {
    this.setState({
        ...
    })
}).catch(function (error) {
    console.log(error);
});

I get an error "XMLHttpRequest cannot load https://api.darksky.net/forecast/febb2871126cd24613f32a79c32d4158/38.5815719,-121.4943996. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://ebcperez.github.io' is therefore not allowed access."


回答1:


I got a same issue But i solved that

https://www.freecodecamp.org/forum/t/calling-openweathermap-api-is-blocked-due-to-cors-header-access-control-allow-origin-missing/191868

like this : "https://cors-anywhere.herokuapp.com/http://samples.openweathermap.org/data/2.5/forecast?appid={your_Api_key}"

or https://api.openweathermap.org/data/2.5/forecast?appid=

So i don't know that url will be didn't occur error




回答2:


Looks like the Darksky API server doesn't allow CORS, so you won't be able to make this request from your browser.

A possible solution would be to issue the API request from your application server, and then just display it on the front end.




回答3:


Here’s an explanation of why this happens. The tl;dr is to use a CORS proxy for your requests. Prepend https://cors-anywhere.herokuapp.com/ to your API’s URL. https://cors-anywhere.herokuapp.com/http://api.openweathermap.org...

i hope this works for you



来源:https://stackoverflow.com/questions/41215140/weather-api-request-cors-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!