Using jQuery
.ajax()
to read "./ex.html
" returns the expected contents of the file
in the first alert box. Using the same call just changing the request to "./
" does not return an expected directory listing in the second alert box.
<html>
<head>
</head>
<body>
<script type="text/JavaScript" src="jquery.js"></script>
<script type="text/JavaScript">
alert($.ajax({type: "GET", url: 'ex.html', async: false}).responseText);
alert($.ajax({type: "GET", url: '.', async: false}).responseText);
</script>
</body>
</html>
When access the file directly: $ firefox ex.html
the results are as described.
$ firefox .
displays:
Index of file:///home/cwhii/work/jq/ex
Up to higher level directory
Name Size Last Modified
ex.html 1 KB 03/24/2010 10:29:37 PM
jquery.js 161 KB 03/17/2010 05:16:58 PM
Then clicking on the ex.html
link produces the expected file content in the first alert box and the directory listing in the second alert box.
In summary, invoking firefox with the file name on the command line does not produce the directory listing but when navigating to the same file via the link on the directory page the second alert box does display the listing.
Additionally开发者_运维知识库, I invoked Google Chrome browser 5.0.307.11 beta in all the above described ways and all of the results produced no directory listings in the alert boxes even though
$ google-chrome .
produces a directory listing page.
You're going to need some sort of server side code to generate a list of files/folders within a directory. Your web server is most likely redirecting that request to either the default page for the directory (index.html, default.html, etc), or it throwing a 404/403/etc page, since the page you've requested doesn't exist.
Javascript doesn't do "directories" or "files", it only works with URLs. Whatever your server responds with when requesting /
is all Javascript has access to.
If the webserver has directory browsing/listing turned, then this could work.
Ideally you'd create a server-side page/script to return a directory listing as JSON.
You need to write a PHP directory listing script and pass the directory which it shall parse by $.ajax ...
bare jQuery won't be enough to accomplish this.
精彩评论