最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How read json local file with jquery with ajax? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Apr 28, 2012 at 20:30 Ben asked Apr 27, 2012 at 14:50 BenBen 25.8k35 gold badges113 silver badges166 bronze badges 2
  • why the (&callback=?) looks wrong! – ilyes kooli Commented Apr 27, 2012 at 14:52
  • the callback should be used for JSON-P requests – Fabrizio Calderan Commented Apr 27, 2012 at 14:53
Add a ment  | 

2 Answers 2

Reset to default 2

You are missing data variable in callback!

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

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./js/test.json',
   function() {
   alert('1111111111111111');
});
发布评论

评论列表(0)

  1. 暂无评论