Skip to content

Instantly share code, notes, and snippets.

@jorgepinon
Created August 17, 2015 16:51
Show Gist options
  • Save jorgepinon/3302d2a48c0d38f4b713 to your computer and use it in GitHub Desktop.
Save jorgepinon/3302d2a48c0d38f4b713 to your computer and use it in GitHub Desktop.
convert form data to json obj (for ajax post)
function formInputsToJson(form) {
var $form = $(form);
var fArr = $form.serializeArray()
var fObj = {};
for (var i in fArr) {
fObj[fArr[i].name] = fArr[i].value;
}
return fObj;
}
$('form').on('submit', function() {
var dataObj = formInputsToJson(this);
$.ajax({
url: ...,
dataType: 'json',
data: JSON.stringify(dataObj),
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment