Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
Javascript: ¿Cómo configurar la variable para obtener el valor de respuesta de la API?

Aquí está el código con el que estoy trabajando:

 const encodedParams = new URLSearchParams(); encodedParams.append("q", "Hello, world!"); encodedParams.append("target", "es"); encodedParams.append("source", "en"); const options = { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', 'Accept-Encoding': 'application/gzip', 'X-RapidAPI-Host': 'google-translate1.p.rapidapi.com', 'X-RapidAPI-Key': '3955ade317mshe662a97591d523ap1e4475jsn3bb2b223ccbb' }, body: encodedParams }; fetch('https://google-translate1.p.rapidapi.com/language/translate/v2', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));

Aquí está la respuesta que recibo de la API

 {data: {…}} data: translations: Array(1) 0: {translatedText: '¡Hola Mundo!'} length: 1 [[Prototype]]: Array(0) [[Prototype]]: Object [[Prototype]]: Object

¿Cómo tomaría el texto traducido de la respuesta de la API y establecería una variable igual a eso? He probado TRANSLATED_TEXT = response.data.translations[0].translatedText;

Necesito configurar una variable y luego colocar la variable en un h1 en un documento HTML. ¡Cualquier ayuda para obtener la respuesta de la API sería apreciada!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

intente response.data.translations[0].translatedText.value;

o respuesta.datos.translations[0].translatedText[0].text;

about 4 years ago · Juan Pablo Isaza Report

0

Encontré un ejemplo similar: https://rapidapi.com/googlecloud/api/google-translate1/discussions/21334

Prueba: response.data.data['translations'][0]['translatedText']

Espero eso ayude.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!