diff --git a/Docs/Request/Request.md b/Docs/Request/Request.md index bf05d8c4c..62bace7cf 100644 --- a/Docs/Request/Request.md +++ b/Docs/Request/Request.md @@ -30,6 +30,7 @@ An XMLHttpRequest Wrapper. * timeout - (*integer*: defaults to 0) In conjunction with `onTimeout` event, it determines the amount of milliseconds before considering a connection timed out. (It's suggested to not use timeout with big files and only when knowing what's expected.) * headers - (*object*) An object to use in order to set the request headers. * urlEncoded - (*boolean*: defaults to *true*) If set to true, the content-type header is set to www-form-urlencoded + encoding +* responseType - (*string*: defaults to *null*, may be *arrayBuffer* or *blob*) Provides access to the XHR Level 2 *responseType*, allowing requests of binary data. * encoding - (*string*: defaults to 'utf-8') The encoding to be set in the request header. * noCache - (*boolean*; defaults to *false*) If *true*, appends a unique *noCache* value to the request to prevent caching. (IE has a bad habit of caching ajax request values. Including this script and setting the *noCache* value to true will prevent it from caching. The server should ignore the *noCache* value.) * isSuccess - (*function*) Overrides the built-in isSuccess function. @@ -80,17 +81,29 @@ Fired when the Request is making progresses in the download or upload. (This is url: 'image.jpg', onProgress: function(event, xhr){ var loaded = event.loaded, total = event.total; - console.log(parseInt(loaded / total * 100, 10)); } }); myRequest.send(); + var mySound = new Request({ + method: 'get', + url: 'squarepusher.wav', + responseType: 'arraybuffer', + onSuccess: function(res){ + myAudioContext.decodeAudioData(res, function(buffer){ + if (buffer) myAudioBuffer = buffer; + }); + } + }).send(); + ### See Also: - [MDC: nsIDOMProgressEvent](https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIDOMProgressEvent) + - [W3C: XHR Level 2](http://www.w3.org/TR/XMLHttpRequest/) + #### complete Fired when the Request is completed. diff --git a/Source/Request/Request.js b/Source/Request/Request.js index b18ba4435..8ce5f5a49 100644 --- a/Source/Request/Request.js +++ b/Source/Request/Request.js @@ -74,11 +74,22 @@ var Request = this.Request = new Class({ if (progressSupport) xhr.onprogress = xhr.onloadstart = empty; clearTimeout(this.timer); - this.response = {text: this.xhr.responseText || '', xml: this.xhr.responseXML}; - if (this.options.isSuccess.call(this, this.status)) - this.success(this.response.text, this.response.xml); - else - this.failure(); + if (this.options.responseType && (this.options.responseType == 'arraybuffer' || this.options.responseType == 'blob')){ + this.response = {}; + this.response[this.options.responseType] = this.xhr.response || ''; + if (this.options.isSuccess.call(this, this.status)){ + this.success(this.response[this.options.responseType]); + } else { + this.failure(); + } + } else { + this.response = {text: this.xhr.responseText || '', xml: this.xhr.responseXML}; + if (this.options.isSuccess.call(this, this.status)){ + this.success(this.response.text, this.response.xml); + } else { + this.failure(); + } + } }, isSuccess: function(){ @@ -91,6 +102,7 @@ var Request = this.Request = new Class({ }, processScripts: function(text){ + if (typeof text != 'string') return text; if (this.options.evalResponse || (/(ecma|java)script/).test(this.getHeader('Content-type'))) return Browser.exec(text); return text.stripScripts(this.options.evalScripts); }, @@ -198,7 +210,7 @@ var Request = this.Request = new Class({ xhr.open(method.toUpperCase(), url, this.options.async, this.options.user, this.options.password); if (this.options.user && 'withCredentials' in xhr) xhr.withCredentials = true; - + if (this.options.responseType) xhr.responseType = this.options.responseType; xhr.onreadystatechange = this.onStateChange.bind(this); Object.each(this.headers, function(value, key){