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

adding processData option to Request. if set to false, passes the data... #2546

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Docs/Request/Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ An XMLHttpRequest Wrapper.

* url - (*string*: defaults to *null*) The URL to request. (Note, this can also be an instance of [URI][])
* data - (*mixed*: defaults to '') The default data for [Request:send][], used when no data is given. Can be an Element, Object or String. If an Object is passed the [Object:toQueryString][] method will be used to convert the object to a string. If an Element is passed the [Element:toQueryString][] method will be used to convert the Element to a string.
* processData - (*boolean*: defaults to *true*) If set to false, the object passed in via 'data' will be given, as-is, to the underlying XHR object. This can be used to bass binary data through the request (via Blob, Uint8Array, etc).
* format - (*string*: defaults to '') If passed, an additional key 'format' will be appended to 'data' with the passed value. e.g. '&format=json'
* link - (*string*: defaults to 'ignore') Can be 'ignore', 'cancel' and 'chain'.
* 'ignore' - Any calls made to start while the request is running will be ignored. (Synonymous with 'wait': true from 1.11)
Expand Down
32 changes: 20 additions & 12 deletions Source/Request/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var Request = this.Request = new Class({
password: '',*/
url: '',
data: '',
processData: true,
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
Expand All @@ -58,6 +59,11 @@ var Request = this.Request = new Class({
initialize: function(options){
this.xhr = new Browser.Request();
this.setOptions(options);
if ((typeof ArrayBuffer != 'undefined' && options.data instanceof ArrayBuffer) || (typeof Blob != 'undefined' && options.data instanceof Blob) || (typeof Uint8Array != 'undefined' && options.data instanceof Uint8Array)){
// set data in directly if we're passing binary data because
// otherwise setOptions will convert the data into an empty object
this.options.data = options.data;
}
this.headers = this.options.headers;
},

Expand Down Expand Up @@ -156,20 +162,22 @@ var Request = this.Request = new Class({
options = Object.append({data: old.data, url: old.url, method: old.method}, options);
var data = options.data, url = String(options.url), method = options.method.toLowerCase();

switch (typeOf(data)){
case 'element': data = document.id(data).toQueryString(); break;
case 'object': case 'hash': data = Object.toQueryString(data);
}
if (this.options.processData || method == 'get' || method == 'delete'){
switch (typeOf(data)){
case 'element': data = document.id(data).toQueryString(); break;
case 'object': case 'hash': data = Object.toQueryString(data);
}

if (this.options.format){
var format = 'format=' + this.options.format;
data = (data) ? format + '&' + data : format;
}
if (this.options.format){
var format = 'format=' + this.options.format;
data = (data) ? format + '&' + data : format;
}

if (this.options.emulation && !['get', 'post'].contains(method)){
var _method = '_method=' + method;
data = (data) ? _method + '&' + data : _method;
method = 'post';
if (this.options.emulation && !['get', 'post'].contains(method)){
var _method = '_method=' + method;
data = (data) ? _method + '&' + data : _method;
method = 'post';
}
}

if (this.options.urlEncoded && ['post', 'put'].contains(method)){
Expand Down