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

249
Vistas
How would I go about getting a json file into javascipt?

I'm extremely new to javascript, so assume I know nothing. I'm trying to import this json file into my website, and having issues getting it working. Currently, I'm trying out jquery's $.getJSON() method, but it returns with this error code:

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).

I tried looking online about it but nothing useful came up, so here I am. Any help at all would be much appreciated, I've been stuck on this for 2 days now and it's brought my progress to a standstill.

Github

Js

json

html

error

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This issue is because of safety. Because of a security reason you cannot import a Json file in to your code because it contains data that could have some effects on the websites.

Your error caused by :

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

You can use Jquery and ajax to import it.

Step1: use jquery CDN

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

Step2:

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

good luck :)

about 4 years ago · Juan Pablo Isaza Denunciar

0

  1. You cannot import a JSON file in a script tag

  2. You cannot AJAX a JSON file from file system - if you can upload the files to the same webserver (could be a locally run server), then you CAN AJAX it - NOTE the server needs to send correct CORS headers if the server has a different ORIGIN (for example port number)

  3. Alternatively change the JSON file to a JS file:

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

and then import it using

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