XMLHttpRequest
{{APIRef("XMLHttpRequest API")}} {{AvailableInWorkers("window_and_worker_except_service")}}
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.
{{InheritanceDiagram}}
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 {{domxref("EventSource")}} interface. For full-duplex communication, WebSockets may be a better choice.
Constructor
{{domxref("XMLHttpRequest.XMLHttpRequest", "XMLHttpRequest()")}}- : The constructor initializes an
XMLHttpRequest. It must be called before any other method calls.
- : The constructor initializes an
Instance properties
This interface also inherits properties of {{domxref("XMLHttpRequestEventTarget")}} and of {{domxref("EventTarget")}} .
-
{{domxref("XMLHttpRequest.readyState")}}{{ReadOnlyInline}}- : Returns a number representing the state of the request.
-
{{domxref("XMLHttpRequest.response")}}{{ReadOnlyInline}}- : Returns an
{{jsxref("ArrayBuffer")}}, a{{domxref("Blob")}}, a{{domxref("Document")}}, a JavaScript object, or a string, depending on the value of{{domxref("XMLHttpRequest.responseType")}}, that contains the response entity body.
- : Returns an
-
{{domxref("XMLHttpRequest.responseText")}}{{ReadOnlyInline}}- : Returns a string that contains the response to the request as text, or
nullif the request was unsuccessful or has not yet been sent.
- : Returns a string that contains the response to the request as text, or
-
{{domxref("XMLHttpRequest.responseType")}}- : Specifies the type of the response.
-
{{domxref("XMLHttpRequest.responseURL")}}{{ReadOnlyInline}}- : Returns the serialized URL of the response or the empty string if the URL is null.
-
{{domxref("XMLHttpRequest.responseXML")}}{{ReadOnlyInline}}- : Returns a
{{domxref("Document")}}containing 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 Web Workers.
- : Returns a
-
{{domxref("XMLHttpRequest.status")}}{{ReadOnlyInline}}- : Returns the HTTP response status code of the request.
-
{{domxref("XMLHttpRequest.statusText")}}{{ReadOnlyInline}}-
: Returns a string containing the response string returned by the HTTP server. Unlike
{{domxref("XMLHttpRequest.status")}}, this includes the entire text of the response message ("OK", for example).[!NOTE] According to the HTTP/2 specification
{{RFC(7540, "Response Pseudo-Header Fields", "8.1.2.4")}}, HTTP/2 does not define a way to carry the version or reason phrase that is included in an HTTP/1.1 status line.
-
-
{{domxref("XMLHttpRequest.timeout")}}- : The time in milliseconds a request can take before automatically being terminated.
-
{{domxref("XMLHttpRequest.upload")}}{{ReadOnlyInline}}- : A
{{domxref("XMLHttpRequestUpload")}}representing the upload process.
- : A
-
{{domxref("XMLHttpRequest.withCredentials")}}- : Returns
trueif cross-siteAccess-Controlrequests should be made using credentials such as cookies or authorization headers; otherwisefalse.
- : Returns
Non-standard properties
XMLHttpRequest.mozAnon{{ReadOnlyInline}}{{Non-standard_Inline}}- : A boolean. If true, the request will be sent without cookie and authentication headers.
XMLHttpRequest.mozSystem{{ReadOnlyInline}}{{Non-standard_Inline}}- : A boolean. If true, the same origin policy will not be enforced on the request.
Instance methods
{{domxref("XMLHttpRequest.abort()")}}- : Aborts the request if it has already been sent.
{{domxref("XMLHttpRequest.getAllResponseHeaders()")}}- : Returns all the response headers, separated by
{{Glossary("CRLF")}}, as a string, ornullif no response has been received.
- : Returns all the response headers, separated by
{{domxref("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.
- : Returns the string containing the text of the specified header, or
{{domxref("XMLHttpRequest.open()")}}- : Initializes a request.
{{domxref("XMLHttpRequest.overrideMimeType()")}}- : Overrides the MIME type returned by the server.
{{domxref("XMLHttpRequest.send()")}}- : Sends the request. If the request is asynchronous (which is the default), this method returns as soon as the request is sent.
{{domxref("XMLHttpRequest.setAttributionReporting()")}}{{securecontext_inline}}{{experimental_inline}}- : Indicates that you want the request’s response to be able to register an attribution source or trigger event.
{{domxref("XMLHttpRequest.setRequestHeader()")}}- : Sets the value of an HTTP request header. You must call
setRequestHeader()after{{domxref("XMLHttpRequest.open", "open()")}}, but before{{domxref("XMLHttpRequest.send", "send()")}}.
- : Sets the value of an HTTP request header. You must call
Events
This interface also inherits events of {{domxref("XMLHttpRequestEventTarget")}} .
{{domxref("XMLHttpRequest/readystatechange_event", "readystatechange")}}- : Fired whenever the
{{domxref("XMLHttpRequest.readyState", "readyState")}}property changes. Also available via theonreadystatechangeevent handler property.
- : Fired whenever the
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{domxref("XMLSerializer")}}: Serializing a DOM tree into XML- Using XMLHttpRequest
- Fetch API