【问题标题】:Javascript reading server file GET http://localhost:3000/public/uploads/goodj.json 404 (Not Found)Javascript 读取服务器文件 GET http://localhost:3000/public/uploads/goodj.json 404(未找到)
【发布时间】:2017-08-01 02:28:51
【问题描述】:

我正在尝试在 ruby​​ on rails 项目中读取 javascript 中的服务器文件。

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

这会导致错误

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

我认为服务器正在将此请求识别为控制器的操作。

我该怎么做才能让服务器明白这是读取文件的请求?

【问题讨论】:

    标签: ruby-on-rails ajax file-read


    【解决方案1】:

    来自Rails guides

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

    并且 URL 应该是 /uploads/goodj.json,而不是 /public/uploads/goodj.json。所以代码 sn-p 应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 2018-11-22
      • 2019-07-24
      • 1970-01-01
      • 2021-05-19
      • 2019-12-11
      相关资源
      最近更新 更多