Skip to content

Instantly share code, notes, and snippets.

@AlexShkor
Created August 16, 2013 15:07
Show Gist options
  • Save AlexShkor/6250733 to your computer and use it in GitHub Desktop.
Save AlexShkor/6250733 to your computer and use it in GitHub Desktop.
send json as plain form data
var sendJson = function(url, json, callback){
var data = {};
parseValues(json, function(key, value) {
data[key] = value;
});
$.post(url, data,callback);
};
var parseValues = function (data, callback, prefix, postfix) {
postfix = postfix || "";
prefix = prefix || "";
for (var key in data) {
if (data[key] == null) {
continue;
}
if (Object.prototype.toString.call(data[key]) === '[object Array]') {
parseValues(data[key], callback, prefix + key + postfix + "[", "]");
} else if (typeof data[key] == "object") {
parseValues(data[key], callback, prefix + key + postfix + ".");
} else {
callback(prefix + key, data[key]);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment