Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

329
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda