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

111
Views
¿Hay una solución?

Entonces, estoy tratando de ejecutar este código para poner la información del archivo JSON en la página HTML. Este es el código que estoy tratando de ejecutar:

HTML:

 <div id="gameContainer"> <script> var games = "../games.json" document.getElementById("gameContainer").innerHTML = `<h1>${games.author}</h1>` </script> </div>

juegos.json:

 [ { "name": "gameName", "author": "authorName" } ]

En el sitio, el html dice "indefinido" y eso es todo. No hay errores en la consola, nada.

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

0

Primero tendrá que buscar el archivo para leer el contenido del archivo.

 const url = '../games.json'; const fetchJson = async () => { try { const data = await fetch(url); const response = await data.json(); document.getElementById('gameContainer').innerHTML = `<h1>${response[0].author}</h1>`; } catch (error) { console.log(error); } };

Además, ¡tus games son una matriz! Por lo tanto, debe usar un índice de matriz para luego obtener el objeto contenedor como: games[0].author , no games.author .

about 4 years ago · Juan Pablo Isaza Report

0

No puede realizar una llamada AJAX a un recurso local ya que la solicitud se realiza mediante HTTP.

Una solución consiste en ejecutar un servidor web local, entregar el archivo y realizar la llamada AJAX a localhost.

En términos de ayudarlo a escribir código para leer JSON, debe leer la documentación de jQuery.getJSON() :

http://api.jquery.com/jQuery.getJSON

Aquí hay una manera de hacerlo sin jQuery.

Primero crea esta función:

 function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', '../news_data.json', true); xobj.onreadystatechange = function () { if (xobj.readyState == 4 && xobj.status == "200") { callback(JSON.parse(xobj.responseText)); } }; xobj.send(null); }

Entonces puedes usarlo simplemente llamando a algo como esto:

 loadJSON(function(json) { console.log(json); // this will log out the json object });

fuente

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!