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
onreadystatechangeproperty 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. Youronreadystatechangeevent handler function will be invoked as the request proceeds. WhenreadyStateis 4, the response is complete.When readyState is 4, check the
statuscode to ensure that the request was successful. If so, usegetResponseHeader()orgetResponseHeaders()to retrieve values from the response header, and use theresponseTextorresponseXMLproperties 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access