Skip to content

Instantly share code, notes, and snippets.

@wholypantalones
Created January 18, 2013 20:44
Show Gist options
  • Select an option

  • Save wholypantalones/4568369 to your computer and use it in GitHub Desktop.

Select an option

Save wholypantalones/4568369 to your computer and use it in GitHub Desktop.
jQuery post form data to json.
$("form").submit(function(event) {
event.preventDefault();
console.log(JSON.stringify($("form").serializeObject()));
});
});
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment