This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

responseType

Summary

Returns or sets the format the response will be returned in.

Property of apis/xhr/XMLHttpRequestapis/xhr/XMLHttpRequest

Syntax

var result = element.responseType;
element.responseType = value;

Return Value

Returns an object of type

XMLHttpRequestResponseType, which is one of the following:

  • "arraybuffer": an ArrayBuffer
  • "blob": a Blob
  • "document": a Document
  • "json": a JavaScript object, parsed from a JSON string returned by the server
  • "text": a String

Examples

The following code demonstrates to how to use the requestType property of the XMLHttpRequest object to retrieve the results of the XHR request as an ArrayBuffer.

This can be used to load images, audio and other binary data with an XHR request. Once the response is received use the response property to access the ArrayBuffer.

function handler() {
  if (xhr.readyState === 4 /* complete */) {
    if (xhr.status === 200) {
            console.log(xhr.response);
        }
    }
}
var xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.open("GET", "http://localhost/test.ogg", true);
xhr.onreadystatechange = handler;
xhr.send();

Related specifications

W3C XMLHttpRequest Specification
W3C Working Draft

Attributions