4

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."

itsame-ebp
  • 63
  • 1
  • 1
  • 4
  • yep, you'll get that if the server doesn't allow cross origin resource sharing – Jaromanda X Dec 19 '16 at 02:56
  • check it in `chrome`, IE is strict on CORS. – ling7334 Dec 19 '16 at 03:04
  • @ling7334 - all browsers are strict on CORS - there was a setting in chrum that allowed you to turn off CORS logic, but running the browser in that way would mean that you've opened up your system to every known 90's exploit that ever existed – Jaromanda X Dec 19 '16 at 03:08

3 Answers3

3

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

joshua.kim
  • 373
  • 3
  • 11
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.

p4sh4
  • 3,292
  • 1
  • 20
  • 33
0

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

Banny
  • 111
  • 1
  • 6
  • Can you elaborate? You say "here's an explanation of why this happens", but I don't see the actual explanation. – Branco Medeiros Mar 02 '20 at 13:27
  • Hey this solution does not work for me, Please help – mamadgi aishwarya Aug 10 '20 at 11:27
  • may you share yo code snippet and particulary which error are u having? – Banny Aug 11 '20 at 06:35
  • 1
    @Banny Copying and pasting every word from [someone else's answer from a different website](https://forum.freecodecamp.org/t/calling-openweathermap-api-is-blocked-due-to-cors-header-access-control-allow-origin-missing/191868/2) is not a good way to help the issue man. – adamr Apr 19 '22 at 10:48