XMLHttpRequest is a built-in JavaScript class that can be used to pull in content from other documents. It has a response type attribute that indicates what type of data it pulled in and three response attributes for handling different types of responses.

JavaScript code can handle the possible outcomes of the request by assigning functions to the three callback properties of the XMLHttpRequest object:

If the load is successful the response can be accessed in request.onload through one of the three response properties of the XMLHttpRequest object:

These response properties support different translations of the HTTP response body depending on the response mime type and the value of request.responseType attribute. The response mime type is only used to determine whether responseXML uses XML parsing or HTML parsing.

The three response properties of a request are set in accordance with the following table:

request.responseType request.response request.responseText request.responseXML
"" text text document
"text" text text InvalidStateError
"document" document InvalidStateError document
"json" json InvalidStateError InvalidStateError
"arraybuffer" arraybuffer InvalidStateError InvalidStateError
"blob" blob InvalidStateError InvalidStateError

XMLHttpRequest can produce five different translations of the HTTP response:

text translation
a DOMString object resulting from decoding the HTTP response entity body according to its character set (default UTF-8).
document translation
a Document object resulting from parsing the HTTP response entity body following XML or HTML parsing rules.
  • HTML parsing rules only if request.responseType is "document" and the response mime type is "text/html".
  • XML parsing rules if request.responseType is "" or "document" and the response mime type is any of the XML mime types.
In either case the scripts in the loaded document are not executed and style sheets referenced by the loaded document are not loaded. The document translation is null if the parsing fails for any reason.
json translation
a JavaScript object resulting from parsing the HTTP response entity body with the JSON.parse() method, or null if the method throws an exception.
arraybuffer translation
an ArrayBuffer object representing the response entity body.
blob translation
a Blob object representing the response entity body.