Ajax url 获取 404 错误

Ajax url GET 404 error

我正在尝试 Ajax 将一个本地文件夹中的所有图片加载到我的 html 页面上。代码参考 this question。首先在 Eclipse 中的 (Tomcat 8.5) 服务器上的文件 运行,然后我在 Google Chrome 中打开 url。然后 Ajax 根据控制台失败:

获取/Users/jiaqni/.../WebContent/upload404()

知道我做错了什么吗?相对路径 "dir='upload/';" 都不起作用。谢谢你们!

<script>
    $(document).ready(function(){
        console.log("Image appending...");
        var dir = "/Users/jiaqni/.../WebContent/upload/";
        var regexp = new RegExp("\.png|\.jpg|\.jpeg");
        $.ajax({
            url: dir,
            success: function (data) {
                //List all .png .jpg .jpeg file names in the page
                console.log("Success!");
                $(data).find("a").filter(function(){return regexp.test($(this).text());}).each(function(){
                    var filename = this.href.replace(window.location, "");
                    ...
                });
            }
        });
    });
</script>

.htaccess 已添加到文件夹 /User/.../upload/ 以确保其可浏览。如果没有 Ajax,<img src="upload/xxx.jpeg"/> 会显示该文件夹中的图像。

我猜测这里提到的 URL 指的是您计算机上的 local 资源。

不幸的是,这是不可能的 - 通常浏览器(例如 Google Chrome)会阻止您这样做(由于允许它可能会引起隐私和安全问题)。

您应该将文件放在您的网络服务器(例如 Apache、ngnix 等)中,并相应地调整 AJAX 请求的 URL。

祝你好运。