A
A
Andrew2020-11-14 09:06:44
JavaScript
Andrew, 2020-11-14 09:06:44

How to get data from Yandex Weather?

ICE. I can not get weather data from Yandex Weather.
Code:
const key = '04aa3404-32a8-4baf-a280-db8a58bb908a';
const url = ' https://api.weather.yandex.ru/v2/informers?lat=55.... ';
//taken from the example
fetch(url,{
headers:{
'X-Yandex-API-Key': key
}
})
.then(resp => resp.json())
.then(data => console.log( data));
Mistake:
5faf73f08ad95772011178.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2020-11-14
@zkrvndm

Checked, really does not work:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.weather.yandex.ru/v2/informers?lat=55.75396&lon=37.620393');
xhr.setRequestHeader('X-Yandex-API-Key', '04aa3404-32a8-4baf-a280-db8a58bb908a');

xhr.onreadystatechange = function() {
  if (xhr.readyState === XMLHttpRequest.DONE) {
    if (xhr.status === 200) {
      var obj = JSON.parse(xhr.responseText);
      console.log('Успех:');
      console.dir(obj);
    }
    else {
      console.log('Ошибка:');
      console.dir(xhr);
    }
  }
}

xhr.send();

This happens due to the absence of the Access-Control-Allow-Origin header on the Yandex side. You need to contact Yandex technical support and clarify why this header is missing - maybe they just overlooked it and this is a mistake on their part, or maybe it’s conceived that you can’t request weather from the front. In the latter case, you need to proxy the request through your server using either CURL or file_get_contents().
If you don't know what to write to Yandex, just give them a link to my answer, their specialists will understand what it is about.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question