W3Schools HTML DOM Reference, Server Source, Client Source HTTP Response Header viewer

Simple Ajax Examples

Date

Raw response

This example sends no data to the server. The server sends back the date using the PHP date() function as follows: <?php echo date("l dS \of F Y h:i:s A"); ?>

The date is:

Square Root

This example is slightly more complicated than the Date example in that a number is sent to the server, and the server returns the square root of the number. It demonstrates how to make the response from the server depend on something sent from the client.

The processing script is called using onChange. Once you type a number, you'll need to leave the input field for the ajax scripts to execute. Other options are onFocus() and onBlur().

The square root is:

Timer

This example demonstrates the asynchronous nature of ajax, including three independent 5 second timers. Press a button and it will become disabled. Then 5 seconds later it will become reenabled. The timer executes on the host which delays the response for 5 seconds. Note that three different request objects are required to keep the three buttons independent. Try "overlapping" the timers.

Adder

This example uses POST instead of GET to send the two addends.

The sum is:

Address

Raw response

This example retrieves multiple pieces of information from the server: a name, street address, city, state and zip for an individual, in XML format.

The text address is:

The XML address is:

name
street
city, state zip

States

Notes: Sending XML

This example does something completely useless, but in an interesting way. It sends a list of abbreviations and full names of states in XML format to the server, which sends the information back in JSON format. The browser then displays the data on the screen.

XMLHttpRequest Notes

IBM Mastering AJAX Articles

W3 spec

readyState Values

const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
const unsigned short LOADING = 3;
const unsigned short DONE = 4;
readonly attribute unsigned short readyState;

response

readonly attribute unsigned short status;
readonly attribute DOMString statusText;
DOMString getResponseHeader(DOMString header);
DOMString getAllResponseHeaders();
readonly attribute DOMString responseText;
readonly attribute Document responseXML;

getAllResponseHeaders()

Note: The "Get Date" function invokes getAllResponseHeaders()

Free Web Hosting