Javascript 读取服务器文件 GET http://localhost:3000/public/uploads/goodj.json 404(未找到)
Javascript reading server file GET http://localhost:3000/public/uploads/goodj.json 404 (Not Found)
我正在尝试在 rails 项目的 ruby 中读取 javascript 中的服务器文件。
$.ajax({
url: "/public/uploads/goodj.json",
success: function (file_content) {
console.log(file_content);
}
});
这会导致错误
GET http://localhost:3000/public/uploads/goodj.json 404 (Not Found)
我认为服务器正在将此请求识别为控制器的操作。
如何让服务器理解这是读取文件的请求?
来自Rails guides:
config.public_file_server.enabled configures Rails to serve static
files from the public directory. This option defaults to true, but in
the production environment it is set to false because the server
software (e.g. NGINX or Apache) used to run the application should
serve static files instead. If you are running or testing your app in
production mode using WEBrick (it is not recommended to use WEBrick in
production) set the option to true. Otherwise, you won't be able to
use page caching and request for files that exist under the public
directory.
而 URL 应该是 /uploads/goodj.json
,而不是 /public/uploads/goodj.json
。所以代码片段应该是这样的:
$.ajax({
url: "/uploads/goodj.json",
success: function (file_content) {
console.log(file_content);
}
});
我正在尝试在 rails 项目的 ruby 中读取 javascript 中的服务器文件。
$.ajax({
url: "/public/uploads/goodj.json",
success: function (file_content) {
console.log(file_content);
}
});
这会导致错误
GET http://localhost:3000/public/uploads/goodj.json 404 (Not Found)
我认为服务器正在将此请求识别为控制器的操作。
如何让服务器理解这是读取文件的请求?
来自Rails guides:
config.public_file_server.enabled configures Rails to serve static files from the public directory. This option defaults to true, but in the production environment it is set to false because the server software (e.g. NGINX or Apache) used to run the application should serve static files instead. If you are running or testing your app in production mode using WEBrick (it is not recommended to use WEBrick in production) set the option to true. Otherwise, you won't be able to use page caching and request for files that exist under the public directory.
而 URL 应该是 /uploads/goodj.json
,而不是 /public/uploads/goodj.json
。所以代码片段应该是这样的:
$.ajax({
url: "/uploads/goodj.json",
success: function (file_content) {
console.log(file_content);
}
});