【问题标题】:Rails: How to catch ajax successRails:如何捕捉 ajax 成功
【发布时间】:2016-04-17 21:29:54
【问题描述】:

我搜索了很多,但无法找到我的错误。当我刚开始使用 JS 时,这可能很容易。使用:Rails 4

我有一个带有表单的模式,我想在成功(或不成功)执行 ajax 调用后做一些事情。现在只是关闭模式。

模态(这确实有效,所以我可以编辑东西):

.modal.fade{ id: attribute.id, role: 'dialog', tabindex:'-1' }
  .modal-dialog{ role: 'document'}
    .modal-content
      .modal-header
        %button{ type: 'button', class: 'close', 'data-dismiss' => "modal" }
          %span ×
        %h4.modal-title= attribute.name

  = form_for attribute, remote: true, html: { class: 'form-inline', id: 'edit_form' } do |f|
    .modal-body
      .form-group
        = f.label :name, 'Name', class: 'form-label', style: 'width: 150px'
        = f.text_field :name, class: 'form-control', style: 'width:200px;margin-right: 35px'
    .modal-footer
      %button{ type: "button", class: "btn btn-default pull-left", 'data-dismiss' => "modal" }
        Close
      = f.submit 'Create', class: 'btn btn-primary pull-right'

我的 JS 文件(别笑):

$(document).ready( function($) {
        console.log('ready'); # this I *can* see in the console

    $('#edit_form').bind("ajax:success", function() {
      console.log('great success') # this I *can't* see in the console.
    });
});

控制器(另一个有趣的事实:模式是在不同控制器的视图中触发的,而不是我实际尝试更改的模式):

respond_to do |format|
  format.js {}
end

所以最后我想捕捉“错误”和“成功”回调并用它做某事(这是我想先弄清楚自己)。我在这里做错了什么?

非常感谢您的帮助 - 非常感谢!

【问题讨论】:

    标签: javascript ruby-on-rails ajax twitter-bootstrap ruby-on-rails-4


    【解决方案1】:

    根据answer of a similar question你可能想试试:

    $('#edit_form').on("ajax:success", ...
    //             ^-- instead of .bind
    

    我假设你也有一个最近的 jQuery 版本,所以试试这个改变。

    【讨论】:

    • 迟到总比不到好;)
    猜你喜欢
    • 2013-09-02
    • 1970-01-01
    • 2012-11-22
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    相关资源
    最近更新 更多