XMLHttpRequest — An HTTP request and response

Inherits from



The XMLHttpRequest object allows client-side JavaScript to issue HTTP requests and receive responses (which need not be XML) from web servers. XMLHttpRequest is the subject of Chapter 18, and that chapter contains many examples of its use.

Create an XMLHttpRequest object with the XMLHttpRequest() constructor (see the sidebar in Using XMLHttpRequest for information on how to create an XMLHttpRequest object in IE6) and then use it like this:

  1. Call open() to specify the URL and method (usually “GET” or “POST”) for the request.

  2. Set the onreadystatechange property to the function that will be notified of the progress of the request.

  3. Call setRequestHeader(), if needed, to specify additional request parameters.

  4. Call send() to send the request to the web server. If it is a POST request, you can also pass a request body to this method. Your onreadystatechange event handler function will be invoked as the request proceeds. When readyState is 4, the response is complete.

  5. When readyState is 4, check the status code to ensure that the request was successful. If so, use getResponseHeader() or getResponseHeaders() to retrieve values from the response header, and use the responseText or responseXML properties to obtain the response body.

XMLHttpRequest defines a relatively high-level interface to the HTTP protocol. It takes care of details such as handling redirects, managing cookies, and negotiating cross-origin connections ...

Get JavaScript: The Definitive Guide, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.