简体   繁体   English

如何使用纯JavaScript且不使用其他库或框架来读取文本文件或XML文件

[英]How to read the text file or XML file using pure JavaScript and using no other library or framework

I am trying to read the text file or an XML file but I am stuck. 我正在尝试读取文本文件或XML文件,但是卡住了。

How to pass the file name that I have to read. 如何传递我必须阅读的文件名。 Once I get the contents of the file I am sure I will need to use regex but I am stuck on getting the file name and data inside it. 一旦获得文件的内容,我确定我将需要使用正则表达式,但是我一直在获取文件名和数据。

I am sure how to do this in jQuery, but I need to try this out using pure JavaScript and nothing else. 我确定如何在jQuery中执行此操作,但是我需要使用纯JavaScript尝试此操作,而没有其他操作。 Any pointers or suggestions would be of great help. 任何指示或建议都会有很大帮助。

It is not possible to reliably read a filename using JavaScript only, due to security reasons. 出于安全原因,无法仅使用JavaScript可靠地读取文件名。 Even JQuery will fail at some point (Google Chrome tends to block Ajax calls to the local machine). 甚至JQuery有时也会失败(Google Chrome浏览器会阻止对本地计算机的Ajax调用)。

EDIT: An example of a JSON config object: 编辑:JSON配置对象的示例:

var config =
{
    color: "blue",
    time: "15:00",
    amount: 1,
    names: ["Bob", "John"]
}

var someName = config.names[1] //John

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

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