Posting a form as a JSON (JQuery)
After hours of searching… Here is a dirty solution I found.
1. Add this function to the prototype.
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
Then use this conversion when posting.
JSON.stringify($("form").serializeObject())
JSON.stringify comes from json2.js. You can get a copy at http://www.json.org/json2.js
Hope this helps someone.
Filed under: Uncategorized on June 18th, 2010
Leave a Reply