Why doesn't json response come from the server via the fetch function?

  • 0
    Good afternoon, trying to send a request that returns json. The request seems to go away, but there is no answer. Where did I go wrong?

    fetch("https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&hl=ru&dt=t&dt=bd&dj=1&source=icon&tk=467103.467103&q=привет", {
        method: "POST",
    }).then(function(response) {
        // Стоит проверить код ответа.
        if (!response.ok) {
            // Сервер вернул код ответа за границами диапазона [200, 299]
            return Promise.reject(new Error(
                'Response failed: ' + response.status + ' (' + response.statusText + ')'
        // Далее будем использовать только JSON из тела ответа.
        return response.json();
    }).then(function(data) {
           let slovo = data.sentences[0].trans;
    }).catch(function(error) {
        // ... Обрабатываем ошибки.

    should output to the console Hello. But it outputs Promise {} ....
    JavaScript Sofia Glenn, Jul 10, 2020

  • 2 Answers
  • 0
    It is not clear why you are trying to send a POST request when the appearance of your link screams that it is GET.

    In general, it will be correct, execute in the console:

    (await (await fetch('https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&hl=ru&dt=t&dt=bd&dj=1&source=icon&tk=467103.467103&q=привет')).json()).sentences[0].trans;

  • 0
    Very strange. If you do not specify init, then everything works:

    If specified, it issues Response failed, which is probably logical because POST instead of GET. But the promise was not achieved.
    Mia Ramirez

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