0

我有两个名为client/ordersand的控制器notary/orders。在“客户/订单”中有 click_me 链接。当我单击链接时,我应该将 ajax 请求传递到notary/orders/pending_orders页面reload the notary/orders/pending_orders

这是我在 client/orders/new 中的 Ajax 调用

<script>
    jQuery.noConflict();
jQuery(document).ready(function(){
alert(9);
jQuery(".test_link").live("click", function(){
  alert("am clicked");
      jQuery.ajax({
      url: "/notary/orders/pending_orders",
      dataType: 'script',
      type: "get",
      data: {data:'hi'},
      success:function(){
          alert("success");
      },
      error:function(){
          alert("failure");   
      }
    });
})

});

<%= link_to "click me", "javascript:void(0);" , :class => "test_link" %>

这是我的代码notary/orders/pending_orders

if request.xhr?
   respond_to do |format|
   format.js { render :js=>'location.reload();',
   :location => "notary/orders/pending_orders"}
   end
    end

但它client/orders/new不会重新加载notary/orders/pending_orders。但我想重新加载公证/订单/pending_orders。我该怎么做?

4

1 回答 1

1

我猜链接<%= link_to "click me", "javascript:void(0);" , :class => "test_link" %> 在里面client/orders/new,你想notary/orders/pending_ordersclient/orders/new.

由于您是从 发出请求client/orders/new,因此您的所有响应都只会得到更新client/orders/new

似乎您正在client/orders/new从一个浏览器执行操作并在另一个浏览器中侦听页面notary/orders/pending_orders以在后端发生某些更改时更新通知。

notary/orders/pending_orders要在后端发生更改时自动更新页面(例如:在 中显示通知),我用来做的是,我会为此设置一个单独的侦听器,

    <script type="text/javascript">
      jQuery(document).ready(function(){
        notificationListener();
      }); 

      function notificationListener(){
        jQuery.ajax({
           url: "/notary/orders/pending_orders",
           type: "get",
           data: {data:'yourdata'},
           success:function(){
             if(expected result){
                //code to update browser view
             }
             else{
                setTimeout(notificationListener(),10000)
             }
           },
           error:function(){
              //
           }
      });
    }

</script>
于 2013-06-07T04:56:24.223 回答