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

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

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 Report

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