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.
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 Isikov9 Answers
You can use serializeArray
[docs] and add the additional data:
An alternative solution, in case you are needing to do this on an ajax file upload:
OR even simpler.
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
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'sdata
param.
Example code:
Eric WangEric WangYou 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:
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.
MathlightNot the answer you're looking for? Browse other questions tagged javascriptjquery or ask your own question.
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 Reference6 Answers
Use this function. This will work only with jquery.
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
Jquery Serialize
A modern interpretation. babel stage-2 preset is required to compile the object spread operator
synthet1csynthet1cYou can use: jquery.serializeToJSON - https://github.com/raphaelm22/jquery.serializeToJSONIts is prepared to work with forms ASP MVC