Execute server side ruby file using javascript - javascript

I have a ruby file on my server (a simple test file). I am wondering how I can use javascript to execute that file on server with a button click. Nothing fancy I want here like rails or asp or php. Its just a small test and I want to see how far it goes. On terminal, all I have to do is ruby test.rb. Is there a way to do this with javascript on an html page?

Adapt the script so your webserver can access it with a Rack or CGI.
Then you just need to make an HTTP request to it. You could set location = URI, call submit() on a form, use XMLHttpRequest or any of a host of other options.

Well browsers don't run Ruby, so I think you need to send a request with that button click to your server, and then the server will run the file.
Try Sinatra: http://www.sinatrarb.com/

Related

Run a script in a google cloud from a local html page

I would like to run a script in a google cloud server using a local HTML page.
To be more clear the steps would be:
open a local HTML page on my local computer.
push a button that triggers a script in my google cloud server.
the script creates a file in the server that I can download pressing
another button.
I'm new in this field and I don't know where to start.
How do I connect to the server via HTML? (PHP?, Javascript?)
How does the authorization process work?
There are several languages and strategies that you can use.
You can use locally Javascript or PHP (it needs installation and configuration) that will allow you, for example, to make an HTTP request (it may also be another protocol), to a script (that can be in PHP or Javascript or others) on the server, which, upon receiving the request, processes and generates a file for a specific path.
Then on that other button you make a request to that path to download the file.
My suggestion is to choose the languages and implement with these to understand the process.
Create an HTML page, put a button.
Attach a function to button onClick to send an Ajax request
That would cause cross server request challenge for you down the road..
You can simply put a URL from your local web page styled as a button to your Google Cloud hosted application.
Create the file on the server side and you can set an HTML header
Content-Disposition: attachment; filename="results.csv"
to make a file downloaded to the user end.

Best Practice: Call .Net Function after webpage is visible for the user

I'm in search of some best practices. I have a webpage asp.net (a loading page as you will).
Once this page is loaded (and visible to the user) i want to execute a function that might last some seconds.
problem is, how do i launch this function?
I've tried loadComplete (event),unload (event) , working with timeout (javascript to poll itself with json) and even the timer-class (.Net). Could anyone point out an alternative/ best pratices to perform this kind of action?
This won't work the way that you are trying to do it. To understand why you have to understand the concept of webservers:
Webservers use some code (PHP, ASP.NET, ...) to generate dynamical HTML code that is beeing sent to the client. This process works in the following steps (in case of ASP.NET)
Client requests something from the server.
Server generates html (that's your ASP.NET / C# code)
Server sends HTML to client
The browser on the client receives the HTML and displays it.
So your ASP.NET code is only running in step 2. This makes it impossible to catch the event when the client has rendered his page in his browser. However there is client-side technology called AJAX. You can make AJAX calls using javascript (that runs in the browser of the client).
So for example you can create a javascript function that sends a second request to the server when the html was loaded in the client, and executes some function on the server.
For this you will need a webservice on the server that will serve as a interface between the javascript and your C# code.
A easy way to create webservices is to use WCF webservices
If you have any further questions feel free to comment on this answer.

Javascript Get Page Source (server-side)

Is it possible to get the source of a server-side page from a javascript script? I want to be able to get the source of a page that is in the same folder on the server. Is it possible without using anything other than javascript?
If you want to get the result of the execution of some server side script (often HTML) from javascript you could use AJAX.
If you want to get the source code of some server side script using javascript (such as the C#, Java, PHP, ... code), you can forget about it unless you publish this source code as text file so that the server doesn't try to interpret it and then use AJAX to fetch this text file from javascript. Of course anyone will be able to access it, not only javascript hosted on your site.

Run program on tomcat server with Javascript, is it possible?

so, here is the thing, I need to run .jar I've programmed myself on a server.
On the otherside, that server is also a webserver, so it displays webpages and so. Those webpages are written in simple HTML and JavaScript.
So, here is the thing:
is there a way to run my program when users perform click on links in the webpage?
I created a function in Javascript, but I cannot make it work, as long as I don't know how to run shell commands in Javascript.
The execution of the program is totally transparent to the user, and what it basically does is to search content in some documents in order to update the html that is shown to the user.
Any ideas? Hope I made myself clear.
Thanks in advance!
You really can't run shell commands in JavaScript.
If I understand your question correctly, you're looking to do some sort of remote process execution.
Is your JAR file on the other server - is it running as part of a web server, I.E. as a servlet in an application container like Tomcat, such that your code can be executed by calling HTTP methods? (If not, you may want to start out with doing this.) If so, then you'll need to have your JavaScript make the HTTP call to the server to execute its code. A common way of doing this today is through the use of AJAX - and you can use something like jQuery to help with this. The response from this AJAX request (could be XML, JSON, or pre-formatted HTML) could contain the details to "update the HTML that is shown to the user".
The simplest way to do this is put the html, javascript, and a jsp page in the same war file, and then run it on tomcat. Not the best way, but definitely the quickest.
EDIT:
So to clarify, the JSP page will get invoked by the click (ajax call, use jquery), and renders some json/xml. The success handler for the ajax call takes the jsp response and updates your html page. So the jsp runs on the server, but the html/javascript runs in the browser.
If you want to make if fancier, you can use spring web-mvc and write a controller instead of a jsp page. It has the same overall effect either way.

Run cgi script without changing the current html page?

I have a cgi script written in C, which is actually a client program.
What I need to do is when the up arrow key is pressed, I need the cgi program to execute and send data to a C server program residing in my webserver?
How can I exectute cgi scripts without the page changing ?
my C server program is listening on port 5000,
my html page is in http server at port 80. On loading, when the user presses the up arrow key,
the cgi script must run and send a value 1 to the server program. Also my cgi script is a client program.
I have already written the Javascript code to take keyboard inputs and displays particular values.
Running a CGI script is just a matter of making an HTTP request. There are numerous ways to do this without leaving the page.
Some of the more common ones are:
Load the URL in an (i)frame
Have the script return an image and dynamically generate an <img> element
Use XMLHttpRequest to make the request
Have the script return a piece of JS and dynamically generate an <script> element
Most of these come under the general heading of Ajax (and XHR being the most well associated method). There are plenty of libraries that help with this, and most big libraries (such as YUI and jQuery) include Ajax helper methods.
You want to communicate with the server without having to reload the page? Use AJAX (javascript framework).

Categories

Resources