Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post a complex HTML form as JSON?

I have a very complex form in a web page. User actually builds up a complex object. The UI is handled using jQuery (showing, hiding sections, duplicating and deleting sub-forms,...).

Simply posting the form upon user submission, although possible, doesn't seem the best solution: It would be hard to makeup unique names for fields (there can be arrays of objects) and to decode the whole stuff on server side.

I guess I should rather post a JSON representation of the object. How do I do that?

I'm not trying to make an ajax call. I want to submit the form but using JSON instead of an usual application/x-www-form-urlencoded form.

FWIW, the backend is ASP.NET MVC.

TIA,

like image 244
Serge Wautier Avatar asked Mar 02 '11 11:03

Serge Wautier


1 Answers

Serialise to JSON using script...

Although this answer uses Ajax, you could instead write the serialised string to an input[type=hidden] in your form

Serializing to JSON in jQuery

like image 196
jenson-button-event Avatar answered Sep 18 '22 18:09

jenson-button-event