A
A
alkostarr2018-11-09 12:43:19
JavaScript
alkostarr, 2018-11-09 12:43:19

Yandex translator does not accept text from HTML?

Good afternoon, ladies and gentlemen, a problem arose, thanks to which I attained the dignity of a great martyr. The essence of the problem is in the YANDEX TRANSLATOR API. The catch is that the translated text must be output to the query, for example

var xhr = new XMLHttpRequest;
var key ='key api';
var api = 'https://translate.yandex.net/api/v1.5/tr.json/translate';
var textTo = document.getElementById('lermontov');
var poesi = textTo.innerHTML; но почему то текст туда не парсится есть ли у кого советы полный код сейчас такой:var xhr = new XMLHttpRequest;
var key ='trnsl.1.1.20181108T113030Z.ae2987ad813d2c91.9f2475ed51dfcbbe42878727b644716ee9';
var api = 'https://translate.yandex.net/api/v1.5/tr.json/translate';
var textTo = document.getElementById('lermontov');
var poesi = textTo.innerHTML;
// console.log(textTo);
alert(Object.keys(poesi)[0]);
var language ='&lang=ru-en';
var url = this.api+'?';
url += 'key=' + this.key;
url += '&text='+ this.textTo;
url += language;
console.log(url)
xhr.open('GET',url,true);
xhr.onreadystatechange= function(){
    if(xhr.readyState == 4){
        if(xhr.status == 200){
  console.log(JSON.parse(this.responseText))
        }
    }
}
xhr.send()

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victor L, 2018-11-09
@alkostarr

var textTo = document.getElementById('lermontov');
var poesi = textTo.innerHTML;
var xhr = new XMLHttpRequest;
var key ='Ваш ключ -35d211642e6820a07c07d47303a';
var api = 'https://translate.yandex.net/api/v1.5/tr.json/translate';
var textTo = document.getElementById('lermontov');
var poesi = textTo.innerHTML;
var language ='&lang=ru-en';
var url = api+'?';
url += 'key=' + key;
url += '&text='+ encodeURIComponent(textTo.innerHTML);
url += language;
xhr.open('GET',url,true);
xhr.onreadystatechange= function(){
    if(xhr.readyState == 4){
        if(xhr.status == 200){
  console.log(JSON.parse(this.responseText))
         document.getElementById('result').innerHTML += this.responseText;
        }
    }
}
xhr.send()

V
Victor P., 2018-11-09
@Jeer

If it does not accept layout, then do not shove layout into it.
Try
var poesi = textTo.innerText;

A
alkostarr, 2019-01-04
@alkostarr

I did the same but thank you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question