rails3 rails.js and jquery catching success and failure of ajax requests

后端 未结 3 826
星月不相逢
星月不相逢 2020-12-04 15:52

Previously, in rails 2.3.8 i used the prototype-helpers link_to_remote and form_remote_for (amongst others).

These had the option :u

3条回答
  •  清歌不尽
    2020-12-04 16:39

    The related rails 4 guide can be found at: http://guides.rubyonrails.org/working_with_javascript_in_rails.html

    It points to the documentation of the events at: https://github.com/rails/jquery-ujs/wiki/ajax , as mentioned by ncherro

    The actual values passed to the callbacks can be inferred from jQuery's ajax method http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings

    .bind is deprecated in favor of .on by jQuery: http://api.jquery.com/on/

    So now the recommended approach is:

    Template:

    <%= link_to 'Click me!',
        'path/to/ajax',
        remote: true,
        id: 'button',
        method: :get,
        data: {type: 'text'}
    %>
    

    CoffeScript:

    $(document).ready ->
      $("#button").on("ajax:success", (e, data, status, xhr) ->
        alert xhr.responseText
      ).on "ajax:error", (e, xhr, status, error) ->
        alert "error"
    

提交回复
热议问题