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:
request.responseType
is
"document"
and the response mime type is
"text/html"
.
request.responseType
is ""
or "document"
and the response
mime type is any of the XML mime types.
null
if the parsing
fails for any reason.
null
if the method
throws an exception.