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

271
Visualizações
Javascript read local JSON file (Same-Origin-Policy Error)

I'm currently building a small local website for my company. All files are stored on our server and you can access the site just by opening up the index.html (the URL would be something like "X:/folder/subfolder/index.html").

The website mostly contains images, pdf's, and tables (it's kind of a database for company internal knowledge). All of the information for the tables are stored in multiple JSON-files and I want to read those files with Javascript and display their content in the html-file.

I tried everthing with the Liveserver in Visual Studio Code and it worked perfectly fine, but when I open up the actual html file in Firefox/Chrome it will show me an Cross-Origin Error "...Same-Origin-Policy blocked reading file...bla bla bla..."

Is there a way to access those files?

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script defer src="main.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
    <h1>JSON Reader</h1>
</body>
</html>

main.js

window.onload = function() {
    JsonReader("../data.json");
}

function JsonReader(jsonPath) {  
    fetch(jsonPath)
    .then((response) => response.json())
    .then(function(jsonData) {
        console.log(jsonData)
    });
}

data.json

{
  "key-1": "value-1",
  "key-2": "value-2",
  "key-3": "value-3",
  "key-4": "value-4",
  "key-5": "value-5"
}
about 4 years ago · Juan Pablo Isaza
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