以 JSON 形式提交表单(无 AJAX)
是否可以在不使用 AJAX 的情况下以 JSON 形式提交表单数据?
我尝试更改 enctype:
<form enctype="application/json"></form>
但这不是一个有效的值,根据 w3schools
原因我希望这种行为是请求的 URL 将返回一个文件,如果我使用 AJAX,我显然无法对其执行任何操作。我想发送标记为 Content-Type: application/json
的 JSON 数据,以便 ASP.NET MVC 使用其 JSON 绑定。
Is it possible to submit form data as JSON, without using AJAX?
I've tried changing the enctype:
<form enctype="application/json"></form>
But that's not a valid value according on w3schools
The reason I would like this behaviour is that the requested URL will return a file, which I obviously can't do anything with if I use AJAX. I would like to send JSON data marked as Content-Type: application/json
so that ASP.NET MVC will use its JSON binding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,您可以使用插件像对象一样序列化表单。我给你写了一个样本;
//Head
您可以从此处下载插件
//Form
//JS
祝你好运!
Yes, you can serialize form like an object with plugin. I write a sample for you;
//Head
You can download plugin from here
//Form
//JS
Good luck!
您可以使用 JSON.stringify() 序列化您的客户端对象,然后将其填充到隐藏输入中并提交您的表单...然后在控制器端将其从 Request.Form 中拉回并将其反序列化到您的对象中?
[编辑]
刚刚在原始问题下面的评论部分看到,这本质上是重复的帖子,并且这个 stackoverflow 是一个解决方案。
Could you use JSON.stringify() to serialize your client side object and then stuff that into a hidden input and submit your form...and then in the controller side pull it back out of the Request.Form and deserialize it into your object?
[Edit]
Just saw in the comment section underneath of the original question that this was essentially a duplicate post and that this stackoverflow worked as a solution.
你可以尝试;
// html
// dto
// 主控制器
you can try;
// html
// dto
// home controller
根据 W3C 标准,您不能使用
Description
传递JSON 等数据只要表单的
enctype
属性设置为application/json
,此规范
就会从表单传输JSON
数据。在过渡期间,不支持此编码的用户代理将回退
以使用application/x-www-form-urlencoded
。这可以在服务器端检测
,并且可以使用本规范
中描述的转换算法
将此类数据转换为JSON
。输入名称
中使用的路径格式很简单。首先,当不存在结构化信息时,信息将简单地被捕获为JSON
对象参考文档
As per W3C standards you can't pass the data like JSON using
<form enctype="application/json"></form>
Description
User agents that implement this
specification
will transmitJSON
data from their forms whenever the form'senctype
attribute is set toapplication/json
. During the transition period, user agents that do not support this encoding willfall back
to usingapplication/x-www-form-urlencoded
. This can bedetected
on the server side, and theconversion algorithm
described in thisspecification
can be used to convert such data toJSON
.The path format used in
input names
is straightforward. To begin with, when no structuring information is present, the information will simply be captured askeys
in aJSON
objectReference DOC
您现在可以根据 2014 年 5 月 29 日发布的新 W3C 标准设置表单 enctype='application/json'。
您可以检查它: http://www.w3.org/TR/html-json-forms/
You can now set form enctype='application/json' according to the new W3C standards published on 29 May 2014.
You can check it : http://www.w3.org/TR/html-json-forms/
你可以使用Form2js。它是由google设计的,很容易使用的库。
https://github.com/maxatwork/form2js
另外,它可以根据用户需求进行修改。可以检查他们的许可证。您可以使用以下链接找到此 javascript 文件的基本示例:
http://form2js.googlecode.com/hg/example/test.html
You can use Form2js.It is designed by google and it is easy to use library.
https://github.com/maxatwork/form2js
Also , It can be modified according to user requirement .You can check their license .You can Find the basic examples of this javascript file using the link below:
http://form2js.googlecode.com/hg/example/test.html
尝试这个简单的方法,将您的 POST 数组存储在变量中,然后将其编码为 json obj。
像这样-->
抱歉,它是 PHP 的
Try this simple store your POST array in variable and then encode it as json obj.
like this-->
Sorry its for PHP