Javascript读取本地/(与html相同的机器)xml文件

时间:2021-12-26 15:07:24

My goal is to read in a file test.xml which is in the same folder as my index.html.

我的目标是读入一个文件test.xml,它与index.html位于同一个文件夹中。

I know that I can pull in script files with something like <script src="index.js></script>, but is there a way for me to do the same with an xml file <xml src="test.xml"><xml>.

我知道我可以使用类似

I do not want to use the AJAX XMLHttpRequest because the index.html may not be hosted on an http server, but instead opened like a normal file in chrome.

我不想使用AJAX XMLHttpRequest,因为index.html可能不会托管在http服务器上,而是像chrome中的普通文件一样打开。

Do I have any options when it comes to opening up this xml file, or will it have to be hosted on a http server? I guess another option would be to store the whole xml file inside of a javascript variable, but this seems unnecessary.

在打开这个xml文件时,我有任何选择,还是必须在http服务器上托管?我想另一种选择是将整个xml文件存储在javascript变量中,但这似乎没必要。

1 个解决方案

#1


0  

do not want to use the AJAX XMLHttpRequest because the index.html may not be hosted on an http server, but instead opened like a normal file in chrome.

不想使用AJAX XMLHttpRequest,因为index.html可能不会托管在http服务器上,而是像chrome中的普通文件一样打开。

Try launching chrome with --allow-file-access-from-files flag set . Should then be able to use XMLHttpRequest to retrieve file in local filesystem

尝试使用--allow-file-access-from-files标志集启动chrome。然后应该能够使用XMLHttpRequest来检索本地文件系统中的文件

*nix

/usr/bin/google-chrome --allow-file-access-from-files

*indows

start chrome --allow-file-access-from-files

See How do I make the Google Chrome flag “--allow-file-access-from-files” permanent? , Opening Chrome From Command Line

请参阅如何将Google Chrome标记“--allow-file-access-from-files”永久保留? ,从命令行打开Chrome

#1


0  

do not want to use the AJAX XMLHttpRequest because the index.html may not be hosted on an http server, but instead opened like a normal file in chrome.

不想使用AJAX XMLHttpRequest,因为index.html可能不会托管在http服务器上,而是像chrome中的普通文件一样打开。

Try launching chrome with --allow-file-access-from-files flag set . Should then be able to use XMLHttpRequest to retrieve file in local filesystem

尝试使用--allow-file-access-from-files标志集启动chrome。然后应该能够使用XMLHttpRequest来检索本地文件系统中的文件

*nix

/usr/bin/google-chrome --allow-file-access-from-files

*indows

start chrome --allow-file-access-from-files

See How do I make the Google Chrome flag “--allow-file-access-from-files” permanent? , Opening Chrome From Command Line

请参阅如何将Google Chrome标记“--allow-file-access-from-files”永久保留? ,从命令行打开Chrome