How to get data from Yandex Weather?

  • 0
    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 .... ';
    // took the link from the example
    fetch (url, {
    headers: {
    'X-Yandex-API-Key': key
    }
    })
    .then (resp = & gt; resp.json ())
    .then (data = & gt; console.log (data));
    Error:
    JavaScript Anonymous, Aug 25, 2020

  • 1 Answers
  • 0
    Checked, really doesn't 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 heading is missing - perhaps they just overlooked and this is a mistake on their part, or perhaps it is thought that it is impossible to request the 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 in Yandex, just give them a link to my answer, their specialists will understand what it is about.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!