XMLHttpRequest
XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing.
XMLHttpRequest is used heavily in AJAX programming.
Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML.
If your communication needs to involve receiving event data or message data from a server, consider using server-sent events through the EventSource interface. For full-duplex communication, WebSockets may be a better choice.
Note: This feature is available in Web Workers, except for Service Workers
Constructor
XMLHttpRequest()-
The constructor initializes an
XMLHttpRequest. It must be called before any other method calls.
Properties
This interface also inherits properties of XMLHttpRequestEventTarget and of EventTarget.
XMLHttpRequest.onreadystatechange-
An Event handler that is called whenever the
readyStateattribute changes. XMLHttpRequest.readyStateRead only-
Returns an
unsigned short, the state of the request. XMLHttpRequest.responseRead only-
Returns an
ArrayBuffer,Blob,Document, JavaScript object, or aDOMString, depending on the value ofXMLHttpRequest.responseType, that contains the response entity body. XMLHttpRequest.responseTextRead only-
Returns a
DOMStringthat contains the response to the request as text, ornullif the request was unsuccessful or has not yet been sent. XMLHttpRequest.responseType-
Is an enumerated value that defines the response type.
XMLHttpRequest.responseURLRead only-
Returns the serialized URL of the response or the empty string if the URL is null.
XMLHttpRequest.responseXMLRead only-
Returns a
Documentcontaining the response to the request, ornullif the request was unsuccessful, has not yet been sent, or cannot be parsed as XML or HTML. Not available in workers. XMLHttpRequest.statusRead only-
Returns an
unsigned shortwith the status of the response of the request. XMLHttpRequest.statusTextRead only-
Returns a
DOMStringcontaining the response string returned by the HTTP server. UnlikeXMLHttpRequest.status, this includes the entire text of the response message ("200 OK", for example).Note: According to the HTTP/2 specification (8.1.2.4 Response Pseudo-Header Fields), HTTP/2 does not define a way to carry the version or reason phrase that is included in an HTTP/1.1 status line.
XMLHttpRequest.timeout-
Is an
unsigned longrepresenting the number of milliseconds a request can take before automatically being terminated. XMLHttpRequestEventTarget.ontimeout-
Is an Event handler that is called whenever the request times out.
XMLHttpRequest.uploadRead only-
Is an
XMLHttpRequestUpload, representing the upload process. XMLHttpRequest.withCredentials-
Is a boolean value that indicates whether or not cross-site
Access-Controlrequests should be made using credentials such as cookies or authorization headers.
Non-standard properties
XMLHttpRequest.channelRead only-
The channel used by the object when performing the request.
XMLHttpRequest.mozAnonRead only-
Is a boolean. If true, the request will be sent without cookie and authentication headers.
XMLHttpRequest.mozSystemRead only-
Is a boolean. If true, the same origin policy will not be enforced on the request.
XMLHttpRequest.mozBackgroundRequest-
Is a boolean. It indicates whether or not the object represents a background service request.
Event handlers
onreadystatechange as a property of the XMLHttpRequest instance is supported in all browsers.
Since then, a number of additional on* event handler properties have been implemented in various browsers (onload, onerror, onprogress, etc.). See Using XMLHttpRequest.
More recent browsers, including Firefox, also support listening to the XMLHttpRequest events via standard addEventListener() APIs in addition to setting on* properties to a handler function.
Methods
XMLHttpRequest.abort()-
Aborts the request if it has already been sent.
XMLHttpRequest.getAllResponseHeaders()-
Returns all the response headers, separated by CRLF, as a string, or
nullif no response has been received. XMLHttpRequest.getResponseHeader()-
Returns the string containing the text of the specified header, or
nullif either the response has not yet been received or the header doesn't exist in the response. XMLHttpRequest.open()-
Initializes a request.
XMLHttpRequest.overrideMimeType()-
Overrides the MIME type returned by the server.
XMLHttpRequest.send()-
Sends the request. If the request is asynchronous (which is the default), this method returns as soon as the request is sent.
XMLHttpRequest.setRequestHeader()-
Sets the value of an HTTP request header. You must call
setRequestHeader()afteropen(), but beforesend().
Events
abort-
Fired when a request has been aborted, for example because the program called
XMLHttpRequest.abort(). Also available via theonabortproperty. error-
Fired when the request encountered an error. Also available via the
onerrorproperty. load-
Fired when an
XMLHttpRequesttransaction completes successfully. Also available via theonloadproperty. loadend-
Fired when a request has completed, whether successfully (after
load) or unsuccessfully (afterabortorerror). Also available via theonloadendproperty. loadstart-
Fired when a request has started to load data. Also available via the
onloadstartproperty. progress-
Fired periodically when a request receives more data. Also available via the
onprogressproperty. timeout-
Fired when progress is terminated due to preset time expiring. Also available via the
ontimeoutproperty.
Specifications
| Specification |
|---|
| XMLHttpRequest Standard (XMLHttpRequest) # interface-xmlhttprequest |
Browser compatibility
BCD tables only load in the browser
See also
XMLSerializer: Serializing a DOM tree into XML- MDN tutorials covering
XMLHttpRequest: - HTML5 Rocks — New Tricks in XMLHttpRequest2
- Feature-Policy directive
sync-xhr
