I have spent hours just trying to access a json object stored as a local file in javascript. Basically I am trying to use the json file to initialise a js object with values from the json file. This needs to be done in Chrome. I know that people have had similar problems (relating to security) but I cannot find a way to do this.
The till.js file looks like this:
function Till(){
var mydata = $.getJSON("/files/hipstercoffee.json")
this.shop = mydata[0].shop;
console.log(mydata[0].shop);
}
The json file looks like this:
[
{
"shop": "Coffshop",
"address": "123 Happy Street",
"prices": [
{
"Latte": 2.80,
"Tea": 2.10
}
]
}
]
In a basic index.html file I have the following script tags:
<script type = "text/javascript" src = "src/till.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
I load up the index.html file and in the Chrome developer console I type:
var till = new Till();
The error message I get is:
XMLHttpRequest cannot load file:///files/coffee.json
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
How do I get round this problem?