Created
October 26, 2015 18:01
-
-
Save everdimension/55a441622440d6bd1b37 to your computer and use it in GitHub Desktop.
Append nested object to FormData. The object can be just one level deep. For anything more complex it is recommended to rethink the way you send such data to the server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
if (window.FormData) { | |
FormData.prototype.appendObject = function (obj, namespace) { | |
// EXAMPLE: | |
// var person = { name: 'some name', age: 87 }; | |
// var fd = new FormData(); | |
// fd.appenObject(obj, 'person'); | |
// This appends the keys of the object like this: | |
// fd.append('person[name]', 'some name'); | |
// fd.append('person[age]', 87); | |
var keyName; | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
keyName = [namespace, '[', key, ']'].join(''); | |
this.append(keyName, obj[key]); | |
} | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment