Created
August 16, 2013 15:07
-
-
Save AlexShkor/6250733 to your computer and use it in GitHub Desktop.
send json as plain form data
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
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