function onSubmit( form ){
var data = JSON.stringify( $(form).serializeArray() ); // <-----------
console.log( data );
return false; //don't submit
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form onsubmit='return onSubmit(this)'>
<input name='user' placeholder='user'><br>
<input name='password' type='password' placeholder='password'><br>
<button type='submit'>Try</button>
</form>
var data = {};
$(".form-selector").serializeArray().map(function(x){data[x.name] = x.value;});
var form_data = $('#add_post').serializeArray(); //encode in JSON string
var post_title = form_data[0].value;
var post_description = form_data[1].value;
$(function(){
$('form').on('submit', function(e){
e.preventDefault();
$(this).serializeObject().done(function(o){
if(window.console) console.log(o);
var j = JSON.stringify(o);
alert(j);
//window.open("data:image/png;base64," + o.userfile.data);
});
});
});