Rails中Ajax调用后如何执行JavaScript函数?
我在rails中使用Ajax调用呈现了一些元素,但是我的另一个函数需要引用这些元素。我现在面临的问题是,除非Ajax请求实际完成,否则函数无法获取这些元素。在ajax调用之后,有没有办法在Rails中运行javascript函数 例如: fetch_cards.js.erbRails中Ajax调用后如何执行JavaScript函数?,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,我在rails中使用Ajax调用呈现了一些元素,但是我的另一个函数需要引用这些元素。我现在面临的问题是,除非Ajax请求实际完成,否则函数无法获取这些元素。在ajax调用之后,有没有办法在Rails中运行javascript函数 例如: fetch_cards.js.erb var cards = $('.cards'); // only fetches previous cards, not the new ones my_function( cards ); def fetch_card
var cards = $('.cards'); // only fetches previous cards, not the new ones
my_function( cards );
def fetch_cards
respond_to do |format|
format.js
end
end
cards\u controller.rb
var cards = $('.cards'); // only fetches previous cards, not the new ones
my_function( cards );
def fetch_cards
respond_to do |format|
format.js
end
end
我不知道你的意思,但是javascript提供了监听ajax调用完成的能力。由于您已经使用jQuery,因此可以执行以下操作:
$.ajax('your/resources.json',
complete: function(jqXHR, status){
// this will be executed once the server has completed your request
my_function(cards);
}
);
您可能希望使用更具体的回调函数扩展此函数(
success
,error
),您可以在上找到更多信息。谢谢您的帮助!这几乎修复了它,只是我删除了“url”。“complete:”也缺少“}”。:)是的,这是一个有点草率的例子,我会更新它以供将来参考!