Ajax
  1. The oddly-named Javascript XMLHttpRequest object allows Javascript to send its own HTTP request to a server, and collect the response.
  2. The Javascript can then update the page with the data from the server.
  3. This technique goes by the name AJAX, for “Asynchronous Javascript and XML,” even when no XML is involved.
  4. Here is a simple AJAX [chat pageSource]
    1. Do go ahead and try it, especially during class so you'll have someone to talk to. Enter any handle you like, a message, and send it.
    2. When you send a message, the Javascript makes an HTTP request to a PHP script on the server to deliver the message.
    3. The PHP script stores it in a file.
    4. The Javascript keeps its message area up to date by polling the server twice a second to request new messages.
    5. Messages are numbered internally. The Javascript remembers the number of the last message it received, and asks for all newer ones.
  5. On the server side is this AJAX [message exchange scriptSource].
    1. It does not generate or send HTML and is not intended to be visited in a browser.
    2. It generates either plain text or JSON.
    3. The PHP accepts messages from the Javascript which are sent as request data, and stores them in a file. It responds with just a plain text OK or error message.
    4. It accepts polls and responds with an error or a JSON document reporting any requested messages. (This could be zero messages.)