Permalink
Browse files

add host object check to fix serialization of File/Blobs etc as json

when the content-type is json
  • Loading branch information...
tj committed May 7, 2013
1 parent da0b84e commit b9acbcb8f4178e21e187b1fc1f3904ff65e9a2ff
Showing with 27 additions and 3 deletions.
  1. +27 −3 lib/client.js
View
@@ -3,8 +3,8 @@
* Module dependencies.
*/
-var Emitter = require('emitter')
- , reduce = require('reduce');
+var Emitter = require('emitter');
+var reduce = require('reduce');
/**
* Root reference for iframes.
@@ -20,6 +20,30 @@ var root = 'undefined' == typeof window
function noop(){};
+/**
+ * Check if `obj` is a host object,
+ * we don't want to serialize these :)
+ *
+ * TODO: future proof, move to compoent land
+ *
+ * @param {Object} obj
+ * @return {Boolean}
+ * @api private
+ */
+
+function isHost(obj) {
+ var str = {}.toString.call(obj);
+
+ switch (str) {
+ case '[object File]':
+ case '[object Blob]':
+ case '[object FormData]':
+ return true;
+ default:
+ return false;
+ }
+}
+
/**
* Determine XHR.
*/
@@ -771,7 +795,7 @@ Request.prototype.end = function(fn){
xhr.open(this.method, this.url, true);
// body
- if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data) {
+ if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !isHost(data)) {
// serialize stuff
var serialize = request.serialize[this.getHeader('Content-Type')];
if (serialize) data = serialize(data);

0 comments on commit b9acbcb

Please sign in to comment.