【问题标题】:Detect server down when submit remote: true in Rails远程提交时检测服务器关闭:Rails 中的 true
【发布时间】:2016-10-26 03:44:49
【问题描述】:

我可以检测到客户端何时无法向服务器发送请求吗? (服务器关闭或互联网链接断开或其他东西......)

经验。当我提交表单或单击remote: true 的链接时,如果请求无法到达服务器,我可以向用户显示一条消息。

<%= form_for @item, remote: true do %>
    ....
<% end %>

 or

<%= link_to @item, remote: true %>

【问题讨论】:

    标签: jquery ruby-on-rails


    【解决方案1】:

    您可以为remote: true 请求编写 AJAX 回调,如下所示。

    <script>
      $(document).ready(function(){
        $('#item').on('ajax:success', function(e, data, status, xhr) {
          //Success action
        }).on('ajax:error',function(e, xhr, status, error){
          //Failure action
        });
      });
    </script>
    

    你可以在错误块中写任何你想要的。

    Ref

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2012-01-18
      • 2021-04-13
      • 2011-12-19
      相关资源
      最近更新 更多