如何捕获ajax:success事件处理程序的响应文本?

时间:2011-04-09 15:41:52

标签: ruby-on-rails ruby-on-rails-3 prototypejs

我可能错过了一些明显的东西,但我是来自jQuery的Prototype的新手。我有一个使用link_to和remote => true创建的链接,输出一个正在运行的AJAX链接。

这是我的JS:

Event.observe(window, 'load', function() {
    $$('.checkoff-link').each(function(element) {
        element.observe('ajax:success', successfulCheckOff);
    });
});

// when they tick it off, check it off
function successfulCheckOff(e) {
    // shrink and strike out the text
    var element = e.element();
    var label = $(getLabelIdFromLinkId(element.id));

    label.addClassName('strikeout');
}

如何获取ajax请求的responseText?我试图在成功时从我的控制器传回数据,我不知道如何在JS层中捕获它。

1 个答案:

答案 0 :(得分:1)

我使用这个方便的教程想出了这个: https://github.com/rails/prototype-ujs

基本上,所有响应信息都存储在event.memo中,因此您可以使用以下代码:

var response = e.memo;

然后访问您需要的所有内容。

您可以使用responseText方法从响应中获取HTML / Text / JSON:

var html_return = e.memo.responseText;