Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript XHR列出服务器目录_Javascript_Html_Xml_Xmlhttprequest_Lighttpd - Fatal编程技术网

使用JavaScript XHR列出服务器目录

使用JavaScript XHR列出服务器目录,javascript,html,xml,xmlhttprequest,lighttpd,Javascript,Html,Xml,Xmlhttprequest,Lighttpd,当我问到可以javascript列出服务器上的文件时,每个人的回答都是“javascript无法访问服务器文件系统,因为它是客户端脚本语言”。但我认为答案只是部分正确,因为如果启用了dirlisting,浏览器可以列出服务器目录的内容。因此,我决定尝试解析该输出-当您已经能够以xml格式查看所需的数据时,无需使用cgi。以下是我所做的: 我正在使用lighttpd,而lighttpd.conf中的重要条目包括: dir-listing.activate = "enable"

当我问到可以
javascript
列出服务器上的文件时,每个人的回答都是“
javascript
无法访问服务器文件系统,因为它是
客户端脚本语言”。但我认为答案只是部分正确,因为如果启用了
dirlisting
,浏览器可以列出服务器目录的内容。因此,我决定尝试解析该输出-当您已经能够以
xml
格式查看所需的数据时,无需使用
cgi
。以下是我所做的:

我正在使用
lighttpd
,而
lighttpd.conf
中的重要条目包括:

dir-listing.activate    = "enable"                 #enables directory listing
dir-listing.auto-layout = "disable"                #simplifies the list style
mimetype.assign         = ( ".xml" => "text/xml" ) #deals with xmls
用于测试
XHR
test.xml
如下所示:

<?xml version="1.0"?>
<anchors>
 <a>foo</a>
 <a>bar</a>
</anchors>
test.html
用于创建
XHR
的页面:

<html><head></head><body><script>

 if (window.XMLHttpRequest) var request = new XMLHttpRequest();
 else var request = new ActiveXObject('Microsoft.XMLHTTP');
 request.open('post', 'test.xml', true);
 request.send();
 if (request) request.onreadystatechange = function() alert(request.responseXML.getElementsByTagName('a')[1].childNodes[0].nodeValue);

</script></body></html>

if(window.XMLHttpRequest)var request=new XMLHttpRequest();
else-var-request=new-ActiveXObject('Microsoft.XMLHTTP');
open('post','test.xml',true);
request.send();
if(request)request.onreadystatechange=function()警报(request.responseXML.getElementsByTagName('a')[1].childNodes[0].nodeValue);

所有这些都可以正常工作(您在警报框中得到'foo',但是当我请求时。打开
目录而不是
xml
,我什么也得不到,甚至在错误控制台中也得不到。

您的代码有几个问题。首先,在JavaScript中,您正在访问
request.responseXML
之前的
request.readyState
4
。在这一点上,该属性不应该是可用的,因此您应该得到一些错误。这可以很容易地解决

if (window.XMLHttpRequest) var request = new XMLHttpRequest();
else var request = new ActiveXObject('Microsoft.XMLHTTP');

request.open('post', 'dir/', true);
request.send();

request.onreadystatechange = function() {
    if (request.readyState === 4) {
        alert(request.responseXML.getElementsByTagName('a')[1].childNodes[0].nodeValue);
    }
}
但是,您还有另一个问题。您试图将输出解析为XML,但它不是有效的XML文档,也不是有效的HTML文档。您需要有另一个元素包装输出,才能使其成为有效的XML文档,因为只有一个根节点。此外,
在XML中不受支持,因此也需要删除它。像下面这样的方法可以奏效

<?xml version="1.0" encoding="iso-8859-1"?>
<xml>
<h2>Index of /directory/</h2>
<div class="list">
<table summary="Directory Listing" cellpadding="0" cellspacing="0">
<thead><tr><th class="n">Name</th><th class="m">Last Modified</th><th class="s">Size</th><th class="t">Type</th></tr></thead>
<tbody>
<tr><td class="n"><a href="../">Parent Directory</a>/</td><td class="m"></td><td class="s">- </td><td class="t">Directory</td></tr>
<tr><td class="n"><a href="foo">foo</a></td><td class="m">2015-Jan-03 13:24:12</td><td class="s">39.4K</td><td class="t">application/octet-stream</td></tr>
</tbody>
</table>
</div>
</xml>

request.readyState
被省略,以简化示例代码,并且
xml
文件是由
mod\u目录生成的。因此
服务器模块,因此我无法以任何方式操纵其半错误的输出(
等),但是我可以选择通过使用
request.responseType='document'
解析输出来绕过它,这实际上是我问题的直接答案。非常感谢。
<?xml version="1.0" encoding="iso-8859-1"?>
<xml>
<h2>Index of /directory/</h2>
<div class="list">
<table summary="Directory Listing" cellpadding="0" cellspacing="0">
<thead><tr><th class="n">Name</th><th class="m">Last Modified</th><th class="s">Size</th><th class="t">Type</th></tr></thead>
<tbody>
<tr><td class="n"><a href="../">Parent Directory</a>/</td><td class="m"></td><td class="s">- </td><td class="t">Directory</td></tr>
<tr><td class="n"><a href="foo">foo</a></td><td class="m">2015-Jan-03 13:24:12</td><td class="s">39.4K</td><td class="t">application/octet-stream</td></tr>
</tbody>
</table>
</div>
</xml>
request.open('post', 'dir/', true);
request.responseType = 'document';
request.send();