Using the following code, I'm able to generate an array, but I would like to construct a complex JSON object that looks like this:
<script>
$('document').ready(function() {
var $myform = $("#myform"),
$userData = $myform.find('#userInfo'),
$adressData = $myform.find('#adressInfo'),
$btnSubmit = $myform.find('button');
$btnSubmit.on('click', function (event) {
event.preventDefault();
var formData = $myform.serializeArray(),
obj = {};
for(var i=0;i<$userData.length;i++){
obj[formData[i].name] = formData[i].value;
}
$.ajax({
url: '/create/user',
type: 'post',
contentType: "application/json",
data: $myform.formAsJson(),
success:function(){
alert("Great! Everything's OK!");
},
error: function(){
alert("Booo, something wrong :(");
}
});
return false;
});
})
</script>
Here is the actual JSON structure that I would like to have:
{
"firstName" : "first name ",
"lastName" : "last name",
"email" : "some@test.com",
"pass" : "testitbaby",
"address" : {
"street" : "street",
"zip" : "12345",
"city" : "city",
"country" : "DE"
},
"createDate" : 1445885243494,
"isUserActivated" : false
}