From d9ca55d75a2aff3217634440b1a210e8136f43e9 Mon Sep 17 00:00:00 2001 From: Andrew Danger Lyon Date: Thu, 12 Dec 2013 20:35:28 -0800 Subject: [PATCH 1/3] adding processData option to Request. if set to false, passes the `data` key in the given options, verbatim, to the underlying XHR object. this allows passing of Blob, Uint8Array, etc objects. note that this required a change in Request.initialize that works around setOptions converting the binary data object into an empty Object {}. --- Docs/Request/Request.md | 1 + Source/Request/Request.js | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Docs/Request/Request.md b/Docs/Request/Request.md index 844e139e6..ab700c52b 100644 --- a/Docs/Request/Request.md +++ b/Docs/Request/Request.md @@ -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) diff --git a/Source/Request/Request.js b/Source/Request/Request.js index 5c10524bf..2103be059 100644 --- a/Source/Request/Request.js +++ b/Source/Request/Request.js @@ -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, */*' @@ -58,6 +59,11 @@ var Request = this.Request = new Class({ initialize: function(options){ this.xhr = new Browser.Request(); this.setOptions(options); + if (options.data instanceof ArrayBuffer || options.data instanceof Blob || 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; }, @@ -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)){ From 4fa06a10b615380cffb2a3ee047c542bb26f9027 Mon Sep 17 00:00:00 2001 From: Andrew Danger Lyon Date: Thu, 12 Dec 2013 20:42:00 -0800 Subject: [PATCH 2/3] adding support for FormData in raw data send. --- Source/Request/Request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Request/Request.js b/Source/Request/Request.js index 2103be059..20d7b1ac0 100644 --- a/Source/Request/Request.js +++ b/Source/Request/Request.js @@ -59,7 +59,7 @@ var Request = this.Request = new Class({ initialize: function(options){ this.xhr = new Browser.Request(); this.setOptions(options); - if (options.data instanceof ArrayBuffer || options.data instanceof Blob || options.data instanceof Uint8Array){ + if (options.data instanceof ArrayBuffer || options.data instanceof Blob || options.data instanceof Uint8Array || options.data instanceof FormData){ // 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; From 82b21a0533eafbe15870a31ce70832ded32a3f54 Mon Sep 17 00:00:00 2001 From: Andrew Danger Lyon Date: Thu, 26 Dec 2013 21:51:43 -0800 Subject: [PATCH 3/3] adding resilience for older browsers --- Source/Request/Request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Request/Request.js b/Source/Request/Request.js index 20d7b1ac0..13e1dd741 100644 --- a/Source/Request/Request.js +++ b/Source/Request/Request.js @@ -59,7 +59,7 @@ var Request = this.Request = new Class({ initialize: function(options){ this.xhr = new Browser.Request(); this.setOptions(options); - if (options.data instanceof ArrayBuffer || options.data instanceof Blob || options.data instanceof Uint8Array || options.data instanceof FormData){ + 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;