Loading local JSON file

前端 未结 23 2062
悲哀的现实
悲哀的现实 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:42

    function readTextFile(srcfile) {
            try { //this is for IE
                var fso = new ActiveXObject("Scripting.FileSystemObject");;
                if (fso.FileExists(srcfile)) {
                    var fileReader = fso.OpenTextFile(srcfile, 1);
                    var line = fileReader.ReadLine();
                    var jsonOutput = JSON.parse(line); 
                }
    
            } catch (e) {
    
            }
    }
    
    readTextFile("C:\\Users\\someuser\\json.txt");
    

    What I did was, first of all, from network tab, record the network traffic for the service, and from response body, copy and save the json object in a local file. Then call the function with the local file name, you should be able to see the json object in jsonOutout above.

提交回复
热议问题