How to read an external local JSON file in JavaScript?

前端 未结 22 2470
醉酒成梦
醉酒成梦 2020-11-22 02:53

I have saved a JSON file in my local system and created a JavaScript file in order to read the JSON file and print data out. Here is the JSON file:

{"res         


        
22条回答
  •  天命终不由人
    2020-11-22 03:04

    Since you have a web application, you may have a client and a server.

    If you have only your browser, and you want to read a local file from a javascript that is running in your browser, that means that you have only a client. For security reasons, the browser should not let you do such thing.

    However, as lauhub explained above, this seems to work:

    http://www.html5rocks.com/en/tutorials/file/dndfiles/

    Other solution is to setup somewhere in your machine a web server (tiny in windows or monkey in linux) and with an XMLHttpRequest or D3 library, request the file from the server and read it. The server will fetch the file from the local filesystem, and serve it to you through the web.

提交回复
热议问题