3

I would like to know if there is a way to get multiple external JSON files from a javascript file.

My website runs on a local machine. I have several JSON files on this machine :

file1.json :

{ "info1": "foo1", "info2": "foo2", "info3": "foo3", "info4": "foo4"}

file2.json :

{ "info5": "foo5", "info6": "foo6", "info7": "foo7", "info8": "foo8"}

etc..

I would like to access these files in my website with javascript in order to perform a research.

Do you know a way to do this ?

Thanks for your help

Johann

2 Answers 2

5

You can use AJAX for this:

function callback_function(object)
{
    // `object' contains the parsed JSON object
}

var xhr = new XMLHttpRequest();
xhr.open("GET", "/js/file1.json", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        callback_function(JSON.parse(xhr.responseText));
    }
}
xhr.send();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response ! But if I have 50 JSON files to load, am I obliged to do this 50 times ? Is there a way to load a folder of JSON files ?
@Johann You really don't need a server to use jQuery, btw.
0

you should send a request from javascript to your server and retrieve the output of the file. with jQuery you could do following:

jQuery.getJSON(url, function(data){
   // do something
});

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.