XMLHttpRequest: responseType property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since November 2016.
Note: This feature is available inWeb Workers, except forService Workers.
TheXMLHttpRequest propertyresponseType is an enumerated string value specifyingthe type of data contained in the response.
It also lets the author change theresponse type. If an empty string is set as the value ofresponseType, thedefault value oftext is used.
In this article
Value
A string which specifies what type of data the response contains.It can take the following values:
""An empty
responseTypestring is the same as"text", the default type."arraybuffer"The
responseis a JavaScriptArrayBuffercontaining binary data."blob"The
responseis aBlobobject containing the binary data."document"The
responseis anHTMLDocumentorXMLXMLDocument, as appropriate based on the MIME type of the received data. SeeHTML in XMLHttpRequest to learn more about using XHR to fetch HTML content."json"The
responseis a JavaScript object created by parsing the contents of received data asJSON."text"The
responseis a text in a string.
Note:When settingresponseType to a particular value, the author should makesure that the server is actually sending a response compatible with that format. Ifthe server returns data that is not compatible with theresponseType thatwas set, the value ofresponse will benull.
Exceptions
InvalidAccessErrorDOMExceptionAn attempt was made to change the value of
responseTypeonanXMLHttpRequestwhich is in synchronous mode but not in aWorker. For additional details, seeSynchronous XHR restrictions below.
Usage notes
>Synchronous XHR restrictions
You cannot change the value ofresponseType in a synchronousXMLHttpRequest except when the request belongs to aWorker.This restriction is designed in part to help ensure that synchronous operations aren'tused for large transactions that block the browser's main thread, thereby bogging downthe user experience.
XHR requests are asynchronous by default; they are only placed insynchronous mode by passingfalse as the value of the optionalasync parameter when callingopen().
Restrictions in Workers
Attempts to set the value ofresponseType todocument areignored in aWorker.
Specifications
| Specification |
|---|
| XMLHttpRequest> # the-responsetype-attribute> |
Browser compatibility
See also
- Using XMLHttpRequest
- HTML in XMLHttpRequest
- The response data:
response,responseText, andresponseXML