0

How can I read a data from json separate file (f.e. 'data.json') in another file ('index.html') using javascript - in a simplest way ?

When I use it in the same file there is no problem:

data = '[{"name" : "Harry", "age" : "32"}]';            
var j = JSON.parse(data);            
document.getElementById('res').innerHTML = j[0].name;

But I cant manage to do that when I use external json file with:

'[{"name" : "Harry", "age" : "32"}]';

I'm looking for the SIMPLEST way (problably I'm missing some fundamental)

Andrew
  • 3,874
  • 5
  • 39
  • 67

1 Answers1

-1

If you use jQuery, you can do that with getJson function - http://api.jquery.com/jquery.getJSON/

$.getJSON( "/someDir/test.json", function( data ) {
  console.log(data);
});
steppefox
  • 1,784
  • 2
  • 14
  • 19