在使用目录中的JavaScript加载XML文件 [英] Load xml file in a directory using javascript

查看:96
本文介绍了在使用目录中的JavaScript加载XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript加载XML文件。我用下面的code加载XML文件。下面的编码加载XML文件时,它是在同一个文件夹中。

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.

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");

但我想加载在特定的位置,例如XML文件。 C:/xml/dineshkani.xml

But i want to load the xml file in the particular location eg. c:/xml/dineshkani.xml

如果我使用的编码 xmlhttp.open(GET,C:/xml/dineshkani.xml',false); 这样它不会加载XML文件。有没有一种方法来加载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.

推荐答案

尽管它的名字,XMLHtt prequest可用于非HTTP请求。​​
下面应该工作

Despite its name, XMLHttpRequest can be used for non-HTTP requests.
The following should work

xmlhttp.open("GET",'file:///C:/xml/dineshkani.xml',false);

的结果状态是0表示成功,而不是200。这是因为文件和ftp方案不使用HTTP结果codeS

The result status is 0 for success instead of 200. This is because the file and ftp schemes do not use HTTP result codes.

编辑:然而,一些浏览器,包括谷歌浏览器禁用此默认情况下。它通过与启动Chrome浏览器中启用 - 允许文件访问。 所以,如果你正在寻找一个跨浏览器的解决方案,你应该把XML在服务器的目录。

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.

HTML5文件API不会让你访问整个文件系统,你会得到一个沙盒目录的工作。 链接

HTML5 file api does not let you access the entire filesystem you get a sandboxed directory to work with. Link

参考: MDN页

这篇关于在使用目录中的JavaScript加载XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆