【问题标题】:How do you capture the response text for an ajax:success event handler?如何捕获 ajax:success 事件处理程序的响应文本?
【发布时间】:2011-08-02 03:14:51
【问题描述】:

我可能遗漏了一些明显的东西,但我是 Prototype 的新手,来自 jQuery。我有一个使用 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 层中捕获它。

【问题讨论】:

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


    【解决方案1】:

    我使用这个方便的教程解决了这个问题: https://github.com/rails/prototype-ujs

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

    var response = e.memo;
    

    然后访问您需要的一切。

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

    var html_return = e.memo.responseText;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多