Javascript Serialize Form Data

  1. Javascript Serialize Form Data To Object
  2. Jquery Serialize

You can create an auxiliar form using jQuery with the content of another form and then add thath form other params so you only have to serialize it in the ajax call. The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. It is primarily intended for use in sending form data, but can be used independently from forms in order to transmit keyed data.

Active1 year, 2 months ago

Okay, so I'm trying to find out if it's possible to post serialize() and other data that's outside the form.

Here's what I though would work, but it only sends 'wordlist' and not the form data.

Anyone have any ideas?

Vitaliy IsikovVitaliy Isikov
1,7479 gold badges28 silver badges41 bronze badges

9 Answers

You can use serializeArray[docs] and add the additional data:

Felix KlingFelix Kling
593k136 gold badges907 silver badges971 bronze badges
Javascript Serialize Form DataMichael MiorMichael Mior
22.4k7 gold badges65 silver badges100 bronze badges

An alternative solution, in case you are needing to do this on an ajax file upload:

OR even simpler.

r3wtr3wt

When you want to add a javascript object to the form data, you can use the following code

Or if you add the method serializeObject(), you can do the following

Community
GudradainGudradain
2,7251 gold badge20 silver badges34 bronze badges
Ganesan MurugesanGanesan Murugesan

In new version of jquery, could done it via following steps:

  • get param array via serializeArray()
  • call push() or similar methods to add additional params to the array,
  • call $.param(arr) to get serialized string, which could be used as jquery ajax's data param.

Example code:

Eric WangEric Wang
10k8 gold badges83 silver badges112 bronze badges

You could have the form contain the additional data as hidden fields which you would set right before sending the AJAX request to the corresponding values.

Another possibility consists into using this little gem to serialize your form into a javascript object (instead of string) and add the missing data:

Community
Darin DimitrovDarin Dimitrov
878k233 gold badges3076 silver badges2792 bronze badges
Marouane B.Marouane B.

I like to keep objects as objects and not do any crazy type-shifting. Here's my way

if you can't see from above I used the .concat function and passed in an object with the post variable as 'name' and the value as 'value'!

Hope this helps.

Mathlight
3,46310 gold badges50 silver badges95 bronze badges
A A KarimA A Karim

Not the answer you're looking for? Browse other questions tagged javascriptjquery or ask your own question.

Active1 year, 8 months ago

I have a cshtml like the following

In my javascript(in a separate javascript file), I'm trying to serialize this form and convert it into a JSON object.

prints out

And

gives me:

I have tried doing this

But I get this error:

Null ReferenceNull Reference
5,22333 gold badges91 silver badges157 bronze badges

6 Answers

Hitesh SiddhapuraHitesh Siddhapura
BITS_PythonBITS_Python

Use this function. This will work only with jquery.

RifaideenRifaideen

You can use this for make a json with all fields of form.

Jquery example:

Pure JavaScript with FormData:

Javascript Serialize Form Data To Object

Willian RayWillian Ray

Jquery Serialize

A modern interpretation. babel stage-2 preset is required to compile the object spread operator

synthet1csynthet1c
4,8322 gold badges11 silver badges28 bronze badges

You can use: jquery.serializeToJSON - https://github.com/raphaelm22/jquery.serializeToJSONIts is prepared to work with forms ASP MVC

Raphael Monteiro NunesRaphael Monteiro Nunes

Not the answer you're looking for? Browse other questions tagged jqueryasp.net-mvcjsonserialization or ask your own question.