我正在使用 webrick 从服务器运行网页。如何从 Web 浏览器捕获事件?我不想通过 JavaScript 来做,但我想通过 webrick 获得响应,并在 ruby 中处理它,也许使用其中一种do_...
方法。如果 JavaScript 是最低要求,那没关系。我不想重定向到不同的页面。我不知道该为事件添加什么值(例如onclick
)。
<input type=button onclick="..." value="Press Me">
我正在使用 webrick 从服务器运行网页。如何从 Web 浏览器捕获事件?我不想通过 JavaScript 来做,但我想通过 webrick 获得响应,并在 ruby 中处理它,也许使用其中一种do_...
方法。如果 JavaScript 是最低要求,那没关系。我不想重定向到不同的页面。我不知道该为事件添加什么值(例如onclick
)。
<input type=button onclick="..." value="Press Me">
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
}
听起来你在描述ajax。尝试:
link_to "target", {:controller => controller, :action => method, }, :remote => true
从这个答案:
Rails 3 中的 link_to_function 消失在哪里?
编辑:抱歉,当您要求使用 Ruby 时,我假设为 Rails。
经过努力,在这个页面的帮助下,我似乎已经接近了我想要的:
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