如何将复杂的HTML表单发布为JSON? [英] How to post a complex HTML form as JSON?
问题描述
我在网页中有一个非常复杂的表单。用户实际上构建了一个复杂的对象。使用jQuery处理UI(显示,隐藏部分,复制和删除子表单,......)。
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.
我想我应该发布一个对象的JSON表示。我怎么做?
I guess I should rather post a JSON representation of the object. How do I do that?
我不打算进行ajax调用。我想提交表单,但是使用JSON而不是通常的application / x-www-form-urlencoded格式。
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,后端是ASP.NET MVC。
FWIW, the backend is ASP.NET MVC.
TIA,
推荐答案
使用脚本串行化为JSON ...
Serialise to JSON using script...
虽然这个答案使用了Ajax,但您可以将序列化字符串写入表单中的输入[type = hidden]
Although this answer uses Ajax, you could instead write the serialised string to an input[type=hidden] in your form
这篇关于如何将复杂的HTML表单发布为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!