简体   繁体   中英

Ajax Call back Doesnt respond

I have same problem as this question Ajax callbacks not working with Rails 3.0.5 and jQuery 1.5.1 . I din't understand the answer to it but. I have found what is causing problem but not sure why it is and how to solve it. I am using Kaminari gem.

Please help me out.

In my VIEW:

<div id="paginator">
  <%= paginate @user, params: {id: nil, pgsz: 20}, remote: true %>
</div>

In my Controller :

  @user = Kaminari.paginate_array(@properties, total_count: @search_result.total_count).page(@pg).per(@pgsz)
    respond_to do |format|
      format.js
      format.html
    end

In MY Show.js.erb

$('#list').html("<%=j (render 'search_result_list') %>");
$('#paginator').html("<%=j (paginate(@user, params: {id: nil, pgsz: 20}, remote: true).to_s) %>");

In my another JS file where i monitor the ajax

$('#paginator')
.on("ajax:send",  function () {
  // Run a spinner
  console.log("=====BEfore SEND");
})
.on("ajax:complete",  function (e, data, status, xhr) {
  // Stop the spinner
  console.log("=====Afte SEND");
})

So i figured out that $('#paginator').html("<%=j (paginate(@user, params: {id: nil, pgsz: 20}, remote: true).to_s) %>"); this line is causing not to callback for either ajax:successs or ajax:complete . But if i modify $('#paginator').html to $('#paginator').append callback happens but its not what i need.

UPDATE :

Just found that its not only $('#paginator').html("<%=j (paginate(@user, params: {id: nil, pgsz: 20}, remote: true).to_s) %>"); which is causing but if i just modify it to $('#paginator').html("Hey"); also causes no callback .

Any idea why its happening ?

Try changing the order of the format in your controller.

@user = Kaminari.paginate_array(@properties, total_count: @search_result.total_count).page(@pg).per(@pgsz)
    respond_to do |format|
      format.html
      format.js
    end

I had the similar issue, and adding format.js at the end helped. I also had format.json after html one, not sure if you need this or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM