简体   繁体   English

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

[英]Javascript reading local/(same machine as the html) xml file

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> . 我知道我可以使用<script src="index.js></script>类的东西来提取脚本文件,但是我有办法对xml文件<xml src="test.xml"><xml>同样的处理吗? <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? 在打开此xml文件时,我是否有任何选择,还是必须将其托管在http服务器上? I guess another option would be to store the whole xml file inside of a javascript variable, but this seems unnecessary. 我猜另一个选择是将整个xml文件存储在javascript变量中,但这似乎不必要。

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 . 尝试使用--allow-file-access-from-files标志设置启动chrome。 Should then be able to use XMLHttpRequest to retrieve file in local filesystem 然后应该能够使用XMLHttpRequest来检索本地文件系统中的文件

*nix *尼克斯

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

*indows * indows

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

See How do I make the Google Chrome flag “--allow-file-access-from-files” permanent? 请参阅如何使Google Chrome浏览器标志“ --allow-file-access-from-files”永久保存? , Opening Chrome From Command Line 从命令行打开Chrome

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

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