Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Dynamically loading json content within a HTML Script

I am writing a webpage using HTML and javascript. I have written a script (as seen below) that loads records from a json file. My script is meant to load data from the json file and display like a bootstrap alert. When I first ran it, the script worked, but strangely subsequent tests did not work. Does anyone have any ideas to fix this?

index.html:

<!DOCTYPE html>
<html>
    <head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
        <link type="text/css" href="css/style.css" rel="stylesheet">
        <!-- Script in Question -->
        <script type="text/javascript">
            // This is a way to load data from a JSON file and load it into places in the html document. Based on: https://howtocreateapps.com/fetch-and-display-json-html-javascript/
            fetch('alerts.json')
                .then(function(response){
                    return response.json();
                })
                .then(function (data){
                    appendData(data);
                })
                .catch(function (err) {
                    console.log(err);
                });
            function appendData(data) {
                var container = document.getElementById("alerts");
                for (let i = 0; i < data.length; i++) {
                    var div = document.createElement("div");
                    div.className = "alert alert-primary";
                    div.setAttribute("role", "alert")
                    div.innerHTML = data[i].content;
                    container.appendChild(div)
                }
            }
        </script>
        <title>Guitars</title>
    </head>
    <body>
        <div id="alerts" style="text-align: center;">
        <!-- JSON data appears here -->
        </div>
        <div style="text-align: center;" id="main">
        <!-- Other stuff here -->
        </div>
        <div style="text-align: center;">
        <!-- Other stuff here -->
        </div>
    </body>
</html>

alerts.json:

[
    {"content":"Welcome back, test1!"}, 
    {"content":"Welcome back, test2!"},
    {"content":"Welcome back, test3!"}
]

The code reads the records from alerts.json and inserts them into a div with an id "alerts" - as seen in the <script> tag. Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

OP here. Thanks to user @David, it was suggested to clear out all errors found in the webpage debugging console. Upon clearing these up, the function then worked as intended!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda