javascript - Covert form data to JSON string -


<form name = 'test' >     <input type='text' name = 'login'>     <input type='email' name = 'email'> </form> 

if use json.serialize($(form)).serializearray();
[{"name":"login","value":"a value"},{"name":"email","value":"a email"}] while need {"login":"a login","email":"a email"}. how that??

you can pass <form> formdata(), iterate key, value pairs of formdata instance, set each key , value object property , value

let form = document.forms["test"];    let fd = new formdata(form);    let data = {};    (let [key, prop] of fd) {    data[key] = prop;  }    data = json.stringify(data, null, 2);    console.log(data);
<form name='test'>    <input type='text' name='login' value="a login">    <input type='email' name='email' value="a email">  </form>


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -