使用javascript在目录中加载xml文件

时间:2023-02-06 13:17:20

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文件。

3 个解决方案

#1


1  

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启动Chrome来启用它。因此,如果您正在寻找跨浏览器解决方案,则应将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

#2


0  

Unfortunately, you can't access local files through AJAX.

不幸的是,您无法通过AJAX访问本地文件。

You could try the HTML5 file access API, if you want.

如果需要,您可以尝试HTML5文件访问API。

#3


-1  

I dont think it can be loaded use this link it is useful for xml file loading

我不认为它可以加载使用此链接它对xml文件加载很有用

Load xml

#1


1  

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启动Chrome来启用它。因此,如果您正在寻找跨浏览器解决方案,则应将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

#2


0  

Unfortunately, you can't access local files through AJAX.

不幸的是,您无法通过AJAX访问本地文件。

You could try the HTML5 file access API, if you want.

如果需要,您可以尝试HTML5文件访问API。

#3


-1  

I dont think it can be loaded use this link it is useful for xml file loading

我不认为它可以加载使用此链接它对xml文件加载很有用

Load xml