Permalink
Browse files

JX.Request: Support raw data instead of file

Summary:
http://www.w3.org/TR/XMLHttpRequest2/#the-send-method

The XMLHttpRequest spec allows ##send## to take any of the following:
- ArrayBuffer
- Blob
- Document
- string
- FormData

For example, using FormData allows the caller to send files along with
data. This ends up being a straight rename of the property and
deprecating the file property.

Test Plan:
  var request = new JX.Request('someurl', function() { debugger; });
  var formData = new FormData();
  formData.append('foo', 'bar');
  request
    .setRawData(formData)
    .send();

See that formData is sent as part of the request. (check in web inspector)

Reviewers: epriestley, yungsters, tomo

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3727
  • Loading branch information...
1 parent d3fb352 commit e21082793720ee5cf8aa58ccd1b423d9cce5367e @paulshen paulshen committed Oct 17, 2012
Showing with 31 additions and 6 deletions.
  1. +31 −6 src/lib/Request.js
View
@@ -101,16 +101,16 @@ JX.install('Request', {
var method = this.getMethod().toUpperCase();
if (__DEV__) {
- if (this.getFile()) {
+ if (this.getRawData()) {
if (method != 'POST') {
JX.$E(
'JX.Request.send(): ' +
- 'attempting to send a file over GET. You must use POST.');
+ 'attempting to send post data over GET. You must use POST.');
}
if (this._data) {
JX.$E(
'JX.Request.send(): ' +
- 'attempting to send data and a file. You can not send both ' +
+ 'attempting to send data and raw data. You can not send both ' +
'at once.');
}
}
@@ -157,8 +157,8 @@ JX.install('Request', {
}
if (method == 'POST') {
- if (this.getFile()) {
- xport.send(this.getFile());
+ if (this.getRawData()) {
+ xport.send(this.getRawData());
} else {
xport.setRequestHeader(
'Content-Type',
@@ -348,6 +348,24 @@ JX.install('Request', {
return this;
},
+ setFile : function(file) {
+ if (__DEV__) {
+ JX.$E(
+ 'JX.Request.setFile(): ' +
+ 'DEPRECATED. Use JX.Request.setRawData instead.');
+ }
+ return this.setRawData(file);
+ },
+
+ getFile : function() {
+ if (__DEV__) {
+ JX.$E(
+ 'JX.Request.getFile(): ' +
+ 'DEPRECATED. Use JX.Request.getRawData instead.');
+ }
+ return this.getRawData();
+ },
+
setDataWithListOfPairs : function(list_of_pairs) {
this._data = list_of_pairs;
return this;
@@ -422,7 +440,14 @@ JX.install('Request', {
* @param string HTTP method, one of "POST" or "GET".
*/
method : 'POST',
- file : null,
+ /**
+ * Set the data parameter of transport.send. Useful if you want to send a
+ * file or FormData. Not that you cannot send raw data and data at the same
+ * time.
+ *
+ * @param Data, argument to transport.send
+ */
+ rawData: null,
raw : false,
/**

0 comments on commit e210827

Please sign in to comment.