0

I want to load some data from a json file, but the content doesn't show up and also the browser console doesn't return any error. Here's the script:

(function($){
  $(window).load(function(){
    $.getJSON('./assets/js/data.json', function(datas){
        console.log(datas);
    }); //get JSON
  }); //window.load
});

And am calling the script file like:

    <script src="assets/js/ajax.js"></script>

What could be wrong?

--------------more info about issue

when i use $(function(){}) AND $(window).load() chrome dont return any error in console, but the data.json not issued in network tab. and when i don't use $(function(){}) AND $(window).load() the data.json is issued but cant load and the chrome return below errors: chrome returned error in console

bobsilon
  • 1,648
  • 4
  • 20
  • 33

3 Answers3

0

If you assets folder is in root then change you code to(remove leading dot(.)) change './assets/js/data.json' path to '/assets/js/data.json'

    (function($){
      $(window).load(function(){
          $.getJSON('/assets/js/data.json', function(datas){
          console.log(datas);
        }); //get JSON
      }); //window.load
    });
Rajesh Kumar
  • 2,443
  • 3
  • 28
  • 43
0

Remove function($) then try it like below:

$(window).load(function(){
    $.getJSON('./assets/js/data.json', function(datas){
        console.log(datas);
    }); //get JSON
  });
Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35
0

oh god KILL me, all of this issue happened for only a brain crash. i just missed to remove a "," in the data.json.

thanks to all for answers.

bobsilon
  • 1,648
  • 4
  • 20
  • 33