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

242
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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