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

326
Visualizações
Javascript/Html: How to import local json / understanding module access

I am new to javascript / html and have some problems to understand the import / module functionality. I need to load a json file into javascript.

The best way I could find is via import, like:

  import myJason from './dummy.json' assert {type: 'json'};

When trying it, I am getting following error:

  Cannot use import statement outside a module

So I placed it into a module (tried header and body). But I am not able to access the module functions.

Google could not help me, could please somebody explain how to do it properly.

Note: HTML and JSON files are local files (not accessed via Webserver)

Here is the (reduced) code:

    <html lang="en">
        <head>
            <script type="module">
                import myJason from './dummy.json' assert {type: 'json'};
                function dummy(){
                    //evaluate JSON
                    return result();
                }
                window.dummy = dummy;
                export{dummy}

            </script>
        </head>
        <body>


            <script>
                // tried (and other things)
                //   dummy();
                //  window.dummy();
            </script>
        </body>
    </html>





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

0

Try using XML HttpRequest

var xhttp = new XMLHttpRequest();

function dummy(){
    if (this.readyState == 4 && this.status == 200) {
       return JSON.parse(xhttp.responseText)
    }
}

xhttp.onreadystatechange = dummy
xhttp.open("GET", "/dummy.json", true);
xhttp.send();

or even better the fetch api check that here

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