【问题标题】:Capturing events on a web page在网页上捕获事件
【发布时间】:2023-03-04 01:10:01
【问题描述】:

我正在使用 webrick 从服务器运行网页。如何从 Web 浏览器捕获事件?我不想通过 JavaScript 来做,但我想通过 webrick 获得响应,并在 ruby​​ 中处理它,也许使用do_... 方法之一。如果 JavaScript 是最低要求,那没关系。我不想重定向到不同的页面。 我不知道该为事件添加什么值(例如onclick)。

<input type=button onclick="..." value="Press Me">

【问题讨论】:

    标签: ruby events httpresponse webrick ruby-1.9


    【解决方案1】:

    AFAIK,Webrick 是 Ruby 中的 HTTP 服务器。为了从事件处理程序与 HTTP 服务器通信,您需要编写一些 JavaScript 代码。例如:

    <input type=button onclick="javascript:send(this);" value="Press Me">
    

    然后定义send()函数:

    function send(event) {
       // Create Ajax request to the server
    }
    

    【讨论】:

    • 我想看看除了这种方式我还能怎么做。
    【解决方案2】:

    听起来你在描述 ajax。试试:

    link_to "target", {:controller => controller, :action => method, }, :remote => true
    

    来自这个 SO 答案:

    Where did link_to_function disappear to in Rails 3?

    编辑:抱歉,当您要求使用 Ruby 时,我认为是 Rails。

    【讨论】:

    • 我没有使用 Rails。我只是想自己做。但如果我能知道它是如何在 Rails 中实现的,那会有所帮助。
    【解决方案3】:

    经过努力,在this page的帮助下,我似乎已经接近我想要的了:

    xxx.html

    <input type="button" value="Click me" onclick="onclick('value to pass');" />
    

    xxx.js

    function onclick(v){
      e=new XMLHttpRequest();
      e.open("GET", "http://localhost:3000?t=0&"+v, true);
      e.onreadystatechange=function(){if (e.readyState==4 && e.status==200){
        // some code to rewrite if necessary
      };};
      e.send();
    }
    

    xxx.rb

    class WEBrick::HTTPServlet::AbstractServlet
      def do_GET request, response
        # some code to process the request
        response.body = a_return_string_for_rewriting
        response.status = 200
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 2010-09-25
      相关资源
      最近更新 更多