O'Reilly logo

JavaScript: The Definitive Guide, 6th Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

XMLHttpRequest — An HTTP request and response

Inherits from

EventTarget

Synopsis

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required