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 b开发者_如何学Goy 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.
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.
// assuming your form has id=myform // defines a callback on submission of the form #myform. $("#myform").submit(function() { //posts the contents of the form to /action using ajax $.post("/action", $("#myform").serialize(), function(result){ // assuming result is a string of the updated data in html // and assuming that your data goes in an element with the id data-table $("#data-table").html(result) }); return false; // prevents the form from submitting normally });
jquery docs for
- submit
- post
- serialize
- html
I want to […] send a command via (the server) […] without switching to a different page
This is known as Ajax and is usually implemented using XMLHttpRequest.
精彩评论