0

I started learning JSON and Javascript and want to create a very easy project with Apache Cordova. So the HTML code should run on my mobile device.

When trying to access the data from the JSON file

{
    "test": [
        {
            "number": 9,
            "name": "testObject1",
            "types": "death, fire",
            "locations": "forest",
            "evolesAt": 2,
            "evolvesTo": "testGen"
        },
        {
            "number": 28,
            "name": "testObject2",
            "types": "poison",
            "locations": "graveyard",
            "evolesAt": 54,
            "evolvesTo": "endboss"
        }
    ]
}

by using this code (the json file is on the same directory level as the javascript code)

$.getJSON("TestDatabase.json", function (json) {
    console.log(json);
});

the console says

Failed to load file:///.../TestDatabase.json: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

So do I have to install nodeJS for the mobile device? Do I have to use a different data format (means no JSON)?

Currently I just got some HTML, CSS and Javscript/Jquery running in my local browser but I want to run the project on my mobile device later on.

Question3r
  • 2,166
  • 19
  • 100
  • 200
  • 3
    You cannot access the local filesystem directly from a browser, not like that anyway. `getJSON` is for network requests. – Mörre Oct 01 '17 at 20:27
  • 1
    You don't need to install Node, but a webserver. If you're using windows IIS comes as standard, you just need to enable it – Rory McCrossan Oct 01 '17 at 20:28
  • 1
    Actually, since this is Cordova, no node.js is required, just use Cordova's File API: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html – Mörre Oct 01 '17 at 20:29
  • At Chrome, Chormium you can request files from `file:` protocol by launching with `--allow-file-access-from-files` flag set, see [Read local XML with JS](https://stackoverflow.com/questions/41279589/read-local-xml-with-js/) – guest271314 Oct 01 '17 at 20:48

0 Answers0