简体   繁体   English

Rails AJAX(JSON)响应重新加载页面

[英]Rails AJAX (JSON) response reloads the page

In the controller a response to an AJAX Request is following: 在控制器中,对AJAX请求的响应如下:

@response = {resp: "ack"}
render json: @response

JS which handles AJAX is: 处理AJAX的JS是:

$('#some_btn').click(function() {    

    var valuesToSubmit = $('#some_form').serialize();
    var url = $('#some_form').attr('action');

    console.log("VALUE: " + valuesToSubmit);
    console.log("URL: " + search_url);

    $.ajax({
        type: 'POST',
        url: url, //sumbits it to the given url of the form
        data: valuesToSubmit,
        dataType: "JSON",
        success: function(data) {

            console.log("saved");
            console.log(data);

        }
    });

    return false;
});

But the problem is that I don't get console messages, instead the page reloads and I get json as text on a new page. 但是问题是我没有收到控制台消息,而是重新加载页面,并且在新页面上以文本形式获取json。 How to prevent this "non-true-AJAX" behaviour? 如何防止这种“非真实AJAX”行为?

So, I had almost the same problem. 因此,我遇到了几乎相同的问题。 In my case, I was using the folliwing link to send the request: 就我而言,我正在使用Folliwing链接发送请求:

<td>
  <%= link_to add_item_path(item), 
    class: 'ui icon button',
    id: "add-item-#{item.id}",
    method: :post do %>
      <%= fa_icon 'shopping-cart' %>
    <% end %>
 </td>

My js to send the ajax was: 我发送ajax的js是:

  $(document).ready(function() {
    $("a:regex(id,add-item-[0-9]+)").click(function(event) {
      event.preventDefault();
      var link = $(this).attr("href");

      $.ajax({
        url: link,
        method: "POST",
        dataType: "json",
        success: function(data) {
          console.log(data);
          $('#notice-modal').modal('show');
        }
      });
    })
  });

and my rails controller action was: 我的rails controller动作是:

  def add
    @item = Item.find(params[:item_id])
    current_cart << { item_id: @item.id, quantity: 1 }
    render json: {quantity: 1}
  end

So the problem was I using only event.preventDefault() but wasn't enought. 所以问题是我只使用event.preventDefault()但还不够。 For working fine, I need to use event.stopPropagation() . 为了正常工作,我需要使用event.stopPropagation() Like this: 像这样:

  $("a:regex(id,add-item-[0-9]+)").click(function(event) {
    event.preventDefault();
    event.stopPropagation();
    var link = $(this).attr("href");

    $.ajax({
      url: link,
      method: "GET",
      dataType: "json",
      success: function(data) {
        console.log(data);
        $('#notice-modal').modal('show');
      }
    });
  })

The event.stopPropagation() is needed because rails component (rails-ujs I think) sent the request elsewhere. 需要event.stopPropagation() ,因为Rails组件(我认为是rails-ujs)将请求发送到其他地方。 You can also remove the method: :post , and will work fine. 您也可以删除method: :post ,并且可以正常工作。

Hope I helped! 希望我能帮上忙!

Do you need to prevent the default form submit action? 您是否需要阻止默认表单提交操作?

$('#some_btn').click(function(event) {  
  event.preventDefault();

  //...
});

I had this problem myself. 我自己有这个问题。 Turns out, I'd just forgotten to add "//= require jquery_ujs" to my application.js file. 原来,我只是忘了在我的application.js文件中添加“ // = require jquery_ujs”。 As soon as I added it, everything worked fine. 一旦添加它,一切就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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