javascript 使用javascript在目录中加载xml文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11527238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load xml file in a directory using javascript
提问by Dineshkani
I want to load a xml file using the javascript. I use the following code to load the xml file. The below coding loads the xml file when it is in the same folder.
我想使用 javascript 加载一个 xml 文件。我使用以下代码加载 xml 文件。下面的代码在同一文件夹中加载 xml 文件。
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",'dineshkani.xml',false);
xmlhttp.send();
xmlDocument=xmlhttp.responseText;
alert("loaded");
But i want to load the xml file in the particular location eg. c:/xml/dineshkani.xml
但我想在特定位置加载 xml 文件,例如。c:/xml/dineshkani.xml
If i use the coding xmlhttp.open("GET",'c:/xml/dineshkani.xml',false);
like this it wont load the xml file. Is there a way to load the xml file.
如果我使用这样的编码,xmlhttp.open("GET",'c:/xml/dineshkani.xml',false);
它不会加载 xml 文件。有没有办法加载xml文件。
回答by Anirudh Ramanathan
Despite its name, XMLHttpRequest can be used for non-HTTP requests.
The following should work
尽管名称如此,但 XMLHttpRequest 可用于非 HTTP 请求。
以下应该工作
xmlhttp.open("GET",'file:///C:/xml/dineshkani.xml',false);
The result status is 0 for success instead of 200. This is because the file and ftp schemes do not use HTTP result codes.
结果状态是 0 表示成功,而不是 200。这是因为文件和 ftp 方案不使用 HTTP 结果代码。
EDIT:However, some browsers, including Google Chrome disable this by default. It has to be enabled by starting Chrome with --allow-file-access
.
So if you are looking for a cross-browser solution, you should put the XML in your server directory.
编辑:但是,某些浏览器(包括 Google Chrome)默认禁用此功能。必须通过使用--allow-file-access
. 因此,如果您正在寻找跨浏览器的解决方案,您应该将 XML 放在您的服务器目录中。
HTML5 file api does not let you access the entire filesystem you get a sandboxed directory to work with. Link
HTML5 文件 api 不允许您访问整个文件系统,您可以使用沙盒目录来使用它。关联
Reference: MDN Page
参考:MDN 页面
回答by Haroldo_OK
Unfortunately, you can't access local files through AJAX.
不幸的是,您无法通过 AJAX 访问本地文件。
You could try the HTML5 file access API, if you want.
如果需要,您可以尝试HTML5 文件访问 API。