简体   繁体   English

使用JavaScript读取本地JSON文件(无Web服务器)

[英]Read local JSON file (no web-server) with JavaScript

It seems that this has been asked a number of times, but I would like to make sure there are still no other alternatives. 这似乎已被多次询问,但我想确保仍然没有其他选择。

So what I need to do is: 所以我需要做的是:

  1. Generate a .JSON file from .net (easy) 从.net生成.JSON文件(简单)
  2. Read the local JSON file with JavaScript and produce an html page with useful graphs etc (using Raphaeljs). 使用JavaScript读取本地JSON文件并生成包含有用图形等的html页面(使用Raphaeljs)。
  3. Deliver the outcome (HTML + associated JavaScript) to the client. 将结果(HTML +关联的JavaScript)提供给客户端。

I wouldn't mind installing a local Apache to read the JSON file easily but since I cannot ask the client to do this, I am looking for a way to read a local JSON file (on the filesystem, no web server) with JavaScript, so that I eventually provide the client with the HTML, .js and .json and he can see the associated graphs. 我不介意安装本地Apache以轻松读取JSON文件,但由于我不能要求客户端执行此操作,我正在寻找一种方法来使用JavaScript读取本地JSON文件(在文件系统上,没有Web服务器),所以我最终为客户端提供了HTML,.js和.json,他可以看到相关的图形。

I 've been reading that FileReader class is an option but I want to have the flexibility to support these on a number of IE browsers (let say IE7 to IE10). 我一直在读FileReader类是一个选项,但我希望能够灵活地在许多IE浏览器上支持这些(比如IE7到IE10)。

Any help on how to work round this would be much appreciated. 如何解决这个问题的任何帮助将非常感激。

Web browsers are not able to read files from the file system due to security reasons. 出于安全原因,Web浏览器无法从文件系统读取文件。 If I've understood correctly, you have a .NET application running on the local machine? 如果我理解正确,您在本地计算机上运行.NET应用程序?

If this is the case, you can host a WCF service to serve the JSON from your application. 如果是这种情况,您可以托管WCF服务以从您的应用程序提供JSON。 WCF self-hosting is described on this MSDN page . MSDN页面上描述了WCF自托管。 You should be able to make AJAX requests from the browser to a server hosted within you application on localhost. 您应该能够从浏览器向localhost上的应用程序中托管的服务器发出AJAX请求。

If you want to support older IE, you need to go into ActiveX Security setting hell. 如果你想支持旧的IE,你需要进入ActiveX安全设置地狱。 Look into var fso = new ActiveXObject("Scripting.FileSystemObject"); 查看var fso = new ActiveXObject("Scripting.FileSystemObject");

But if you are delivering the content and HTML files to the client, why don't you just add the JSON markup directly into the html document? 但是,如果要将内容和HTML文件提供给客户端,为什么不直接将JSON标记添加到html文档中? Seems like the EASY solution. 看起来像EASY解决方案。

You can't read file from local disk in browser (security restriction). 您无法在浏览器中从本地磁盘读取文件(安全限制)。 But once you are going to deliver HTML + js and JSON (in zip?) you can read json like from regular web server. 但是,一旦你要提供HTML + js和JSON(在zip?中),你可以从常规的Web服务器上读取json。 Just use relative links and if JSON will be in same folder where HTML and JS is located. 只需使用相对链接,如果JSON将位于HTML和JS所在的同一文件夹中。 Or you may put your JSON as a part of HTML you deliver to customer. 或者您可以将您的JSON作为您交付给客户的HTML的一部分。

But if you are going to show a page (HTML and JS) on your site in web, you should better ask client to upload their JSON file, and than download it from server (in AJAX style). 但是,如果您要在Web上的网站上显示页面(HTML和JS),则最好让客户端上传他们的JSON文件,而不是从服务器下载(以AJAX样式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM