Loading local JSON file

前端 未结 23 1627
悲哀的现实
悲哀的现实 2020-11-22 01:28

I\'m trying to load a local JSON file but it won\'t work. Here is my JavaScript code (using jQuery):

var json = $.getJSON("test.json");
var data = e         


        
23条回答
  •  时光说笑
    2020-11-22 01:40

    If you're looking for something quick and dirty just load the data in the head of your HTML document.

    data.js

    var DATA = {"a" : "b", "c" : "d"};
    

    index.html

    
    
       
       
    
    ...
    
    

    main.js

    (function(){
       console.log(DATA); // {"a" : "b", "c" : "d"}
    })();
    

    I should mention that your heap size (in Chrome) is about 4GBs, so if your data is larger than that you should find another method. If you want to check another browser try this:

    window.performance.memory.jsHeapSizeLimit / 1024 / 1024 / 1024 + " GBs"
    // "4.046875 GBs"
    

提交回复
热议问题