0

I try to load a json file from my local project which is not running on a webserver.

test.json

[{
    name : "Google",
    url : "https://www.google.com",
},
{
    name : "Bing",
    url : "https://www.bing.com",                            
}]

First attempt:

First I tried it by using the local file which is inside the project folder.

loadJSON("data/test.json", function(response) {

    console.log(JSON.parse(response));
});

function loadJSON(path, callback) {   

var xobj = new XMLHttpRequest();

    xobj.overrideMimeType("application/json");
    xobj.open('GET', path, true); // Replace 'my_data' with the path to your file
    xobj.onreadystatechange = function () {
        if (xobj.readyState == 4 && xobj.status == "200") {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
        }
    };
    xobj.send(null);  
}

Response:

index.html:616 Failed to load file:///C:/wamp64/www/projects/Startseite/data/test.json: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Then I researched the error, set up a virtual host and changed the call to this:

loadJSON("http://ressources/data/test.json", function(response) {

    console.log(JSON.parse(response));
});

Response:

Failed to load http://ressources/data/test.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Is it even possible to load json without using a webserver or installing browser plugins which changes the header?

3 Answers 3

1

As for JSON you can use JSONP, it's designed for cross-domain and for every url, absolute or relative.

https://www.w3schools.com/js/js_json_jsonp.asp and http://api.jquery.com/jquery.getjson/

test.html

<script>
    function jsonp(data){
      console.log(data)
    }
</script>
<script src="test.js"></script>

test.js

jsonp([{
  name : "Google",
  url : "https://www.google.com",
},
  {
    name : "Bing",
    url : "https://www.bing.com",
  }])
Sign up to request clarification or add additional context in comments.

4 Comments

It seems like this does only work together with a webserver.
@Black it is not required, see modified answer, i've tested it.
but this is a javascript file not a json file. How can I add new objects to the json object now?
@Black The naming of the file doesnt matter. Just generate any javascript array that you want. Than wrap it around a function call. That's jsonp. I'm out here, nothing more i can provide to you. Maybe it doesn't fit your needs, but thats the only way doing this without a webserver.
0

When running javascript on browser I think its impossible to access a file from your filesystem without webservers. If you just wanted to process these files using javascript you can do so by running javascript on your machine using node. Otherwise I am not aware of any other ways, but you may want to take a look at this :

Local file access with javascript

Comments

0

you can't do it like that. If you can't add a web server, one option is to move JSON data into separate .js file. Since your JSON file looks like this:

    [{
        name : "Customer Support OTRS",
        url : "https://support.hecht-international.com",
        desc : "<strong>Customer Support OTRS</strong><br><br>Callagent<br>online",
        img : "img/otrs_thumb.PNG",
    },
{
        name : "hechtAbasConverter",
        url : "https://hac.hecht-international.com/gui/login.php",
        desc : "<strong>hechtAbasConverter - hac</strong><br><br>online",                            
        img : "img/easylife/hac.png",
    }]

move it to jsondata.js

var jsonData = [{
        name : "Customer Support OTRS",
        url : "https://support.hecht-international.com",
        desc : "<strong>Customer Support OTRS</strong><br><br>Callagent<br>online",
        img : "img/otrs_thumb.PNG",
    },
    {
        name : "hechtAbasConverter",
        url : "https://hac.hecht-international.com/gui/login.php",
        desc : "<strong>hechtAbasConverter - hac</strong><br><br>online",                            
        img : "img/easylife/hac.png",
    }]

and load your new file in HTML

<script type="text/javascript" src="path/to/jsondata.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.