使用 Sinatra 和 ruby 连接 HTML 按钮
披露:我对网络编程一无所知
问题背景: 我有一个环境测试室在不同温度下测试嵌入式计算机。它由 Windows 应用程序控制。我可以通过 ruby 和控制应用程序的 Win32API 接口来控制腔室。该会议厅距离我的办公室很远,我可以远程监控状态并通过网络界面触发更改。 Sinatra 可以轻松地通过 Web 服务器显示状态。
我的问题:我想单击状态网页上的按钮并通过 Sinatra 和 Ruby 向应用程序发送命令,而无需切换到其他页面。我找不到任何类似的例子或信息。
答案:您可以使用 Ajax(如已接受的答案中所示),但我将使用帖子并重新加载页面。它简单得多,但不是那么顺利。
Disclosure: I know nothing about web programming
Background to problem:
I have an environmental testing chamber testing embedded computers at various temperatures. It is controlled by a windows application. I can control the chamber via ruby and the Win32API interface of the control application. The chamber is far from my office and I would remotely monitor the state and trigger a change via a Web interface. Sinatra makes it easy to show the state via a web server.
My problem: I want to click on a button on the status webpage and send a command via Sinatra and Ruby to the application without switching to a different page. I cant find any examples or information for something like that.
The Answer: You can use Ajax(as in the accepted answer) but I am going with post and reload the page. It is a lot simpler but not as smooth.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已经有一个可以 POST 到 sinatra 处理程序并执行您想要的操作的表单?
如果是这样,您可以使用 jQuery 轻松使其支持 ajax。
将 jquery.js 添加到您的公共目录中从布局中要求它
添加一些 js 以在单击按钮时发布到处理程序。
Do you already have a form that POSTs to a sinatra handler and does what you want?
If so, you could easily make it ajaxy by using jQuery.
Add jquery.js to your public directory & require it from the layout
Add some js to post to your handler when your button is clicked.
jquery docs for
这称为 Ajax,通常使用 XMLHttpRequest。
This is known as Ajax and is usually implemented using XMLHttpRequest.