List of server directories using JavaScript XHR

When I asked if javascript view files on the server, everyone answered " javascript cannot access the file system of the server because it is client-side scripting language ". But I thought the answer was partially correct, because the browser can display the contents of the server directory if dirlisting is dirlisting . So, I decided to try to parse this conclusion - you do not need to use cgi when you can already see the data that you need in xml format. So here is what I did:

I use lighttpd , and the important elements in lighttpd.conf are:

 dir-listing.activate = "enable" #enables directory listing dir-listing.auto-layout = "disable" #simplifies the list style mimetype.assign = ( ".xml" => "text/xml" ) #deals with xmls 

test.xml used to test XHR is as follows:

 <?xml version="1.0"?> <anchors> <a>foo</a> <a>bar</a> </anchors> 

Directory list page created by lighttpd mod_dirlisting.so :

 <?xml version="1.0" encoding="iso-8859-1"?> <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">&nbsp;</td><td class="s">- &nbsp;</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> 

test.html page used to create 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> 

All of which work fine (you get "foo" in the warning window), but when I request.open instead of xml , I get nothing, even in the error console.

+5
source share
1 answer

You have a couple of problems with your code. First, in your JavaScript, you access request.responseXML until request.readyState is 4 . At this point, this property should not be available, so you should get some errors. This can be fixed quite easily.

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

However, you have one more problem. You are trying to parse your output as XML, but it is not a valid XML document or a valid HTML document. You will need to have another element that completes the output for it as a valid XML document, since there can be only one root root. In addition, &nbsp; is not supported in XML, so it will also need to be removed or replaced . Something like the following will work.

 <?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> 

You may also be interested in using the responseType property for XMLHttpRequest . If you set this property to 'document' , you can parse the HTML response, not the XML response.

 request.open('post', 'dir/', true); request.responseType = 'document'; request.send(); 
+3
source

Source: https://habr.com/ru/post/1210334/


All Articles