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

167
Views
el valor de acceso de JSON por clave usando ajax no muestra el resultado

Tengo datos JSON que se ven así (no es un objeto, es una matriz):

 [ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", }, { "id": 2, "name": "Ervin Howell", "username": "Antonette", "email": "Shanna@melissa.tv", } ]

y una función para obtener datos y mostrarlos usando AJAX, uso JSON.parse() para obtener el JSON de la cadena: Inicialice un objeto XMLHttpRequest:

 let xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState === 4 && this.status === 200) { var jsonObj = JSON.parse(xhttp.responseText);

Agregue un nuevo elemento al documento:

 var table='<tr><th>Name</th><th>Email</th></tr>'; table += '<tr><td>' + jsonObj["name"] + '</td><td>' + jsonObj["email"] + '</td></tr>' document.getElementById('demo').innerHTML = table; } };

Pero cuando ejecuto el navegador no muestra el valor en el campo de la tabla, muestra undefined . ¿Hay algo mal? También cambié para usar JSON.stringtify(), pero el resultado aún no cambió. Por favor, ayúdame a resolver el problema.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debería un ciclo para iterar la matriz como dijo @Lee Taylor.

 for (var item of jsonObj) { table += "<td>" + item.name + "</td>" }
over 4 years ago · Santiago Trujillo 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!