Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XHR Level 2 responseType of arrayBuffer and Blob #2433

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Docs/Request/Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the extra spaces between : and '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.
Expand Down
23 changes: 17 additions & 6 deletions Source/Request/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,21 @@ 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') ){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put it on one line. Also there is an erroneous space at ) ){

this.response = {};
this.response[this.options.responseType] = this.xhr.response || '';
if (this.options.isSuccess.call(this, this.status))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add curly brackets around ifs.

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem.

this.success(this.response.text, this.response.xml);
else
this.failure();
}
},

isSuccess: function(){
Expand All @@ -91,6 +101,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);
},
Expand Down Expand Up @@ -198,7 +209,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){
Expand Down