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.
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 :)
You cannot import a JSON file in a script tag
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)
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>