Ben
Ben

Reputation: 25827

How read json local file with jquery with ajax?

I have js only project. i call from my static index.html file the json file content.

I try(not works):

 $.getJSON('/js/test.json&callback=?',
   function() {
    alert('1111111111111111');
 },'jsonp');

Edit get in chrome: XMLHttpRequest cannot load file:///somefolder/test/data.json. Origin null is not allowed by Access-Control-Allow-Origin

Thanks

Upvotes: 0

Views: 7576

Answers (2)

Sinetheta
Sinetheta

Reputation: 9449

You can retrieve json from a local source, it doesn't need to be jsonp, and even if it did what you are doing is not how you do it.

$.getJSON('http://yours.com/js/test.json',
   function() {
   alert('1111111111111111');
});

Upvotes: 2

Dalius I
Dalius I

Reputation: 1051

You are missing data variable in callback!

$.getJSON('/js/test.json', function(data) {
    console.log('JSON data received:', data);
});

Upvotes: 2

Related Questions