Javascript读取服务器文件GET http:// localhost:3000/public/uploads/goodj.json 404(未找到)

问题描述:

我正尝试在Rails项目的ruby中读取JavaScript中的服务器文件。Javascript读取服务器文件GET http:// localhost:3000/public/uploads/goodj.json 404(未找到)

$.ajax({ 
    url: "/public/uploads/goodj.json", 
    success: function (file_content) { 
     console.log(file_content); 
    } 
}); 

,这会导致错误

GET http://localhost:3000/public/uploads/goodj.json 404(未找到)

我认为服务器识别该请求作为控制器的动作。

如何才能让服务器了解这是读取文件的请求?

Rails guides

config.public_file_server.enabled配置的Rails从公开目录提供静态 文件。此选项默认为true,但在 生产环境中,它被设置为false,因为用于运行应用程序的服务器 软件(例如NGINX或Apache)应该替代地为 服务静态文件。如果您使用WEBrick在 生产模式下运行或测试您的应用(不建议使用 生产中的WEBrick),请将该选项设置为true。否则,您将无法使用 页面缓存和请求存在于公共 目录下的文件。

而URL应该是/uploads/goodj.json而不是/public/uploads/goodj.json。所以代码片段应该如下所示:

$.ajax({ 
    url: "/uploads/goodj.json", 
    success: function (file_content) { 
     console.log(file_content); 
    } 
});