ajax:success and ajax:complete callback doesn't work when using UJS in Rails

后端 未结 4 635
醉梦人生
醉梦人生 2021-01-13 15:43

I have a link that when i click on it will trigger an ajax call then replace this link by another link, says for example the original link is \"add friend\", when i click th

4条回答
  •  死守一世寂寞
    2021-01-13 16:21

    I met this problem before, UJS's ajax:success and ajax:complete won't be fired. But other callbacks work, such as ajax:beforeSend

    My fix is to use jQuery's global event callback to handle it

    $("#my_link").on('ajaxComplete', (evt, data, status, xhr) ->
      alert('This is a jQuery ajax complete event!')
    

    P.S. UJS use jQuery's ajax to send Ajax, so jQuery's callback will surely be fired.

    # https://github.com/rails/jquery-ujs/blob/master/src/rails.js
    ajax: function(options) {
      return $.ajax(options);
    },
    

提交回复
热议问题