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 slightly confused about how AJAX works. I mostly understand the PHP/MySQL model of retrieving database information for the user, but how does AJAX do it?

From what I understand the AJAX engine sends an HTTP request to the backend which does some magic and returns XML data to the AJAX engine which translates it to HTML+CSS for the user to see, but what happens when it sends that first HTTP request to the backend? Is a server-side language acting as the middleman there to get data from the database for the rest of the process?

Also, does HTML5 put any extra spin on the usage of AJAX?

share|improve this question
    
I'm still slightly confused about the server-side part. Is AJAX interacting with .Net/PHP/Java in order to talk to the database? Or can it interact with the database without that middleman? –  Ein Doofus Jun 16 '11 at 12:48

3 Answers 3

Is a server-side language acting as the middleman there to get data from the database for the rest of the process?

Yes. That's exactly what is happening. Just as a browser makes an HTTP request for a page and your backend serves that page up, so does your AJAX code make an HTTP request and your backend serves that up. Whether it's doing database access, processing and then serving an HTML page, or whether it's doing database access, processing and then serving an XML document, there's no massive difference in the flow.

Indeed it's not unknown to retrieve HTML snippets via AJA"X", which are then inserted directly into the page's DOM without needing any translation, which makes the whole process even more similar.

share|improve this answer

the AJAX engine sends an HTTP request

There isn't really an Ajax engine there. It is the browser who initiates a request. Normally to initiate a request a user must perform some action - enter an address in the browser and hit enter, refresh the page, click a link on a page, submit a form. That will lead to the browser replacing the page with a new one, whatever comes as a result of the request.

Now what is known as Ajax is a possibility to initiate such a request from within JavaScript code. That will not cause the browser to replace the page content. You get the response from the server and can do with it whatever you wish. Most typically the response is used to update the structure of the current document (the page you have open in your browser) like injecting extra divs, deleting some, replacing their content with the new received information etc.

For the server it makes absolutely no difference who sent the request. The browser directly, JavaScript code, some other software from wherever on the web - doesn't matter. Server only "thinks" in terms of requests and responses. Server can send anything back as a response - a full HTML page to display in a browser, a piece of random text, some data formatted as XML, data in JSON format, some binary stream (an image or something). It is a very open and a very flexible infrastructure.

Typically partial Ajax requests are used to obtain some partial information to update a portion of the page. But you can come up with any scenario you want.

Usually for a request initiated via JavaScript an extra header is appended to the request:

ajax request

that's how you can find out on the server what kind of request it was (if you need). You could serve a partial response to an Ajax request and a full page to a full request.

P.S. The technique is usually written as Ajax (only the first letter is capitalized). The notation of AJAX has become associated with the Microsoft ASP.NET AJAX library.

share|improve this answer

I'm still slightly confused about the server-side part. Is AJAX interacting with .Net/PHP/Java in order to talk to the database? Or can it interact with the database without that middleman?

In response to this... No as far as I know there are is no way HTTP request can access the database without a programming or scripting language.

The HTTP request does the same as your browser and request the page as you would with your browser.

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.