Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm making a site which should (a) take information from the user in the form of files and forms, (b) take that data to the server to be run on a C application, and (c) take the result back and show it to the user. I was told to look for AJAX for the communication with the server. BTW, I'm using rails.

I'm trying to understand how AJAX works. From what I understand so far, with rails is pretty easy to make the call.

What I can't figure out is, what waits for that call? what process the call?

If I understand correctly, with rails I could make a function in ruby and make it so it's called through AJAX, which means -or so I understand- that it gets executed on the server. If I were using PHP, would I need to make an http server to wait for the AJAX calls?

I just don't find information about what waits for the call, and that information is processed. Any links, comments or books are welcome!

share|improve this question

2 Answers 2

up vote 6 down vote accepted

This is an oversimplified answer, based on what I think your current understanding is. If it's too trivial or too complex, feel free to tell me about it and I'll expand or clarify.

An AJAX request is a normal HTTP request. You are essentially requesting a resource from the web server, and technically it's exactly the same request as if you'd type the URL of the resource in the address bar of your browser.

What's different with AJAX requests is than they can be executed asynchronously. A trivial explanation of what that means is that you don't need to reload the page for the requests to happen.

So, the difference is solely on the client side. On the server side, the HTTP Server doesn't treat them any differently than normal HTTP requests, it listens for requests, and serves resources when asked. These resources can be anything, an HTML script, a PHP script, a Ruby script, any kind of media, etc.

Lastly, your choice of server side language doesn't really matter, the architecture is the same.

share|improve this answer
    
Then probably AJAX is not what I need and I was totally wrong... what would you use to say, make the server execute a system command like "ls" when the user clicks a button? I need to execute a function in C on the server, using the parameters inserted on the forms –  jbcolmenares Oct 31 '12 at 22:58
2  
@jbcolmenares AJAX seems about right for what you're trying to do. I'm not familiar with Ruby, but the general process shouldn't differ much. You need to write a Ruby script to execute the system command. Do that first, and test it by normally requesting it via its URL through your browser. Your AJAX request is simply a request to that URL that you need to attach to the onclick event of the button. When the request completes, it will return a Javascript object that will contain whatever your Ruby script prints in the browser when normally requested. –  Yannis Rizos Oct 31 '12 at 23:03
1  
This answer should be the opening paragraph in every textbook that covers the topic of Ajax. Well said, Mr. Rizos! –  ajax81 Nov 1 '12 at 2:58

I think maybe the point that your missing is that your browser is effectively multi-threaded. When you make an AJAX call, a new thread is spawned which waits for the call to return; while an existing thread sits around performing business as usual (such as responding to user actions).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.