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:
Call
open()
to specify the URL and method (usually “GET” or “POST”) for the request.Set the
onreadystatechange
property to the function that will be notified of the progress of the request.Call
setRequestHeader()
, if needed, to specify additional request parameters.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. Youronreadystatechange
event handler function will be invoked as the request proceeds. WhenreadyState
is 4, the response is complete.When readyState is 4, check the
status
code to ensure that the request was successful. If so, usegetResponseHeader()
orgetResponseHeaders()
to retrieve values from the response header, and use theresponseText
orresponseXML
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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.