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

241
Views
¿Cómo haría para obtener un archivo json en javascipt?

Soy extremadamente nuevo en javascript, así que suponga que no sé nada. Estoy tratando de importar este archivo json a mi sitio web y tengo problemas para que funcione. Actualmente, estoy probando el método $.getJSON() de jquery, pero regresa con este código de error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///C:/Users/edgib102/Documents/GitHub/First-Website/data.json. (Reason: CORS request not http).

Intenté buscar en línea al respecto, pero no apareció nada útil, así que aquí estoy. Cualquier ayuda sería muy apreciada, he estado atascado en esto durante 2 días y ha detenido mi progreso.

Github

js

json

html

error

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

0

Este problema se debe a la seguridad. Por motivos de seguridad, no puede importar un archivo Json a su código porque contiene datos que podrían tener algunos efectos en los sitios web.

Su error causado por:

 <script type="text/javascript" src="data.json"></script>

Puede usar Jquery y ajax para importarlo.

Paso 1: use jquery CDN

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>

Paso 2:

 $.getJSON('dataenter code here.json', function(data) { //do stuff with your data here });

buena suerte :)

about 4 years ago · Juan Pablo Isaza Report

0

  1. No puede importar un archivo JSON en una etiqueta de secuencia de comandos

  2. No puede AJAX un archivo JSON del sistema de archivos; si puede cargar los archivos en el mismo servidor web (podría ser un servidor ejecutado localmente), entonces PUEDE AJAX. NOTA: el servidor debe enviar los encabezados CORS correctos si el servidor tiene un servidor diferente ORIGEN (por ejemplo, número de puerto)

  3. Alternativamente, cambie el archivo JSON a un archivo JS:

     const data = { "object-data": { your data } };

y luego importarlo usando

 <script src="data.js"></script>
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!