XMLHttpRequest.response

The XMLHttpRequest response property returns the response's body content as an ArrayBuffer, Blob, Document, JavaScript Object, or DOMString, depending on the value of the request's responseType property.

Syntax

var body = XMLHttpRequest.response;

Value

An appropriate object based on the value of responseType. You may attempt to request the data be provided in a specific format by setting the value of responseType after calling open() to initialize the request but before calling send() to send the request to the server.

The value is null if the request is not yet complete or was unsuccessful, with the exception that when reading text data using a responseType of "text" or the empty string (""), the response can contain the response so far while the request is still in the LOADING readyState (3).

The response types are described below.

Value Description
"" An empty responseType string is treated the same as "text", the default type (therefore, as a DOMString.
"arraybuffer" The response is a JavaScript ArrayBuffer containing binary data.
"blob" The response is a Blob object containing the binary data.
"document" The response is an HTML Document or XML XMLDocument, as appropriate based on the MIME type of the received data. See HTML in XMLHttpRequest to learn more about using XHR to fetch HTML content.
"json" The response is a JavaScript object created by parsing the contents of received data as JSON.
"text" The response is text in a DOMString object.
"moz-chunked-arraybuffer"

Similar to "arraybuffer", but the data is received in a stream. When using this response type, the value in response is only available in the handler for the progress event, and only contains the data received since the last progress event, rather than the cumulative data received since the request was sent.

Accessing response during a progress event returns the data received so far. Outside the progress event handler, the value of response is always null.

"ms-stream" The response is part of a streaming download; this response type is only allowed for download requests, and is only supported by Internet Explorer.

Example

This example presents a function, load(), which loads and processes a page from the server. It works by creating an XMLHttpRequest object and creating a listener for readystatechange events such that that when readyState changes to DONE (4), the response is obtained and passed into the callback function provided to load().

The content is handled as raw text data (since nothing here is overriding the default responseType).

var url = 'somePage.html'; //A local page

function load(url, callback) {
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      callback(xhr.response);
    }
  }

  xhr.open('GET', url, true);
  xhr.send('');
}
 

Specifications

Specification Status Comment
XMLHttpRequest Living Standard WHATWG living standard

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

See also

Document Tags and Contributors

Contributors to this page: Sheppy, ExE-Boss, erikadoyle, waxter013, MashKao, jsx
Last updated by: Sheppy,