Ruby on rails 无法执行从AJAX请求呈现的js.erb代码
我有一个React应用程序,它通过Axios库向Rails控制器发送POST请求,代码如下所示:Ruby on rails 无法执行从AJAX请求呈现的js.erb代码,ruby-on-rails,ajax,reactjs,axios,Ruby On Rails,Ajax,Reactjs,Axios,我有一个React应用程序,它通过Axios库向Rails控制器发送POST请求,代码如下所示: axios.post(update_url,{ bunch_of_data: data }, {headers: { 'Content-Type': 'application/json' }}) .then(res => { const response_data = res.data; console.log(response_data); }); 控
axios.post(update_url,{
bunch_of_data: data
}, {headers: {
'Content-Type': 'application/json'
}})
.then(res => {
const response_data = res.data;
console.log(response_data);
});
控制器看起来像:
class MyController < ApplicationController
skip_before_action :ensure_logged_in
skip_before_action :authenticate_admin_user
skip_before_action :verify_authenticity_token
def update_feed
respond_to do |format|
format.js { render layout: false }
end
end
end
我的Rails日志最终看起来像:
Started POST "/update_feed" for 127.0.0.1 at 2018-03-26 15:21:23 -0700
Processing by MyController#update_feed as JS
{"format"=>"js", "controller"=>"my_controller", "action"=>"update_feed"}
Rendering /update_feed.js.erb
Rendered /update_feed.js.erb (0.4ms)
Completed 200 OK in 2433ms (Views: 9.0ms | ActiveRecord: 74.1ms)
它说它呈现了js.erb文件,但在Rails端什么也没有发生。但是,我确实在React应用程序内的响应中看到js.erb文件打印中的
警报(!”
(因此我在浏览器控制台中看到警报(!”
)。但是为什么它没有执行?你有udate_feed.js.erb文件吗?里面有什么?我有一个警报,但它只从我的React应用程序中的success块打印出来,那里有console.log。我想..你的代码显然是作为你的控制台输出呈现的。为什么你认为它不起作用?检查浏览器控制台中的“网络响应”选项卡。您希望发生什么情况?我希望从我的js.erb文件在浏览器中弹出一个警报。该文件中没有Javascript正在执行,而是在我对axios成功的响应中打印文件中的代码。是否无法从axios ajax响应触发js.erb代码执行?
Started POST "/update_feed" for 127.0.0.1 at 2018-03-26 15:21:23 -0700
Processing by MyController#update_feed as JS
{"format"=>"js", "controller"=>"my_controller", "action"=>"update_feed"}
Rendering /update_feed.js.erb
Rendered /update_feed.js.erb (0.4ms)
Completed 200 OK in 2433ms (Views: 9.0ms | ActiveRecord: 74.1ms)