Ruby on rails 使用来自Rails API应用程序的脚本和有限的中间件(config.API_only=true)进行响应?
我有一个Rails 5应用程序构建作为一个应用程序。因此,默认情况下,它只响应json 但是对于一个特定的请求,我需要应用程序用脚本响应 在常规的Rails应用程序中,我只需将脚本放在Ruby on rails 使用来自Rails API应用程序的脚本和有限的中间件(config.API_only=true)进行响应?,ruby-on-rails,ruby-on-rails-5,middleware,respond-to,respond-with,Ruby On Rails,Ruby On Rails 5,Middleware,Respond To,Respond With,我有一个Rails 5应用程序构建作为一个应用程序。因此,默认情况下,它只响应json 但是对于一个特定的请求,我需要应用程序用脚本响应 在常规的Rails应用程序中,我只需将脚本放在js.erb文件中。这在这里行不通 如果我的控制器操作如下所示: def respond_with_js end $.getScript("https://myapp.example.com/respond_with_js"); 我请求应用程序如下: def respond_with_js end $.get
js.erb
文件中。这在这里行不通
如果我的控制器操作如下所示:
def respond_with_js
end
$.getScript("https://myapp.example.com/respond_with_js");
我请求应用程序如下:
def respond_with_js
end
$.getScript("https://myapp.example.com/respond_with_js");
它的响应为204无内容
:
Started GET "/respond_with_js" for 127.0.0.1 at 2018-06-27 20:28:44 +0200
Processing by ApplicationController#respond_with_js as */*
Completed 204 No Content in 0ms
如何解决这个问题?如果rails服务器是唯一的api版本,则不能作为脚本请求 Rails默认响应json
def respond_with_json
render json: {message: "works"}, status: :ok
end
要请求它,您需要以json数据类型请求:
$.ajax({
url: "https://myapp.example.com/respond_with_json",
method: "GET",
dataType: "json",
success: function(res){
console.log(res.message)
}
})