Skip to content
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
17 changes: 16 additions & 1 deletion src/js/atomic/atomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@

};

/**
* Check if the provided obj is a FormData object
* @private
* @param {Object|Array|String} obj The object
* @return {Bool}
*/
var isFormData = function(obj) {
return Object.prototype.toString.call(obj) === '[object FormData]';
};

/**
* Parse text response into JSON
* @private
Expand Down Expand Up @@ -106,7 +116,7 @@
var param = function (obj) {

// If already a string, or if a FormData object, return it as-is
if (typeof (obj) === 'string' || Object.prototype.toString.call(obj) === '[object FormData]') return obj;
if (typeof (obj) === 'string' || isFormData(obj)) return obj;

// If the content-type is set to JSON, stringify the JSON object
if (/application\/json/i.test(settings.headers['Content-type']) || Object.prototype.toString.call(obj) === '[object Array]') return JSON.stringify(obj);
Expand Down Expand Up @@ -214,6 +224,11 @@
// Merge options into defaults
settings = extend(defaults, options || {});

// clear Content-Type if provided data is FormData
if (isFormData(settings.data)) {
delete settings.headers['Content-type'];
}

// Make request
return makeRequest(url);

Expand Down
13 changes: 13 additions & 0 deletions test/spec/atomic-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ describe('atomic', function () {
.toHaveBeenCalledWith('Content-type', 'application/json');
});

it('should not be set when using FormData', function() {
atomic.ajax({
url: '/endpoint',
headers: {
'Content-type': 'application/json'
},
data: new FormData()
});

expect(XMLHttpRequest.prototype.setRequestHeader)
.not.toHaveBeenCalledWith('Content-type', 'application/json');
});

});

});