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

328
Views
Javascript/Html: cómo importar json local/acceso al módulo de comprensión

Soy nuevo en javascript / html y tengo algunos problemas para comprender la funcionalidad de importación / módulo. Necesito cargar un archivo json en javascript.

La mejor manera que pude encontrar es a través de la importación, como:

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

Al intentarlo, me sale el siguiente error:

 Cannot use import statement outside a module

Así que lo coloqué en un módulo (probé el encabezado y el cuerpo). Pero no puedo acceder a las funciones del módulo.

Google no pudo ayudarme, por favor alguien podría explicarme cómo hacerlo correctamente.

Nota: los archivos HTML y JSON son archivos locales (no se accede a través del servidor web)

Aquí está el código (reducido):

 <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 answers
Answer question

0

Intenta usar 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();

o incluso mejor, la API fetch comprueba eso aquí

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!