Created
August 17, 2015 16:51
-
-
Save jorgepinon/3302d2a48c0d38f4b713 to your computer and use it in GitHub Desktop.
convert form data to json obj (for ajax post)
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
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