Is it possible to submit form data as JSON, without using AJAX?
是否可以在不使用AJAX的情况下以JSON形式提交表单数据?
I've tried changing the enctype:
我试过更改enctype:
<form enctype="application/json"></form>
But that's not a valid value according on w3schools
但根据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.
我想要这种行为的原因是请求的URL将返回一个文件,如果我使用AJAX,我显然无法做任何事情。我想发送标记为Content-Type:application / json的JSON数据,以便ASP.NET MVC将使用其JSON绑定。
8 个解决方案
#1
1
Yes, you can serialize form like an object with plugin. I write a sample for you;
是的,您可以使用插件将表单序列化为对象。我为你写了一个样本;
//Head
//头
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery.serialize-object.js"></script>
You can download plugin from here
你可以从这里下载插件
//Form
//形成
<form id="frm">
<input type="text" name="Model[Firstname]">
<input type="text" name="Model[Lastname]">
<input type="text" name="ModelDetail[PhoneNumber]">
...
<button type="button" onclick="sendForm()">Send</button>
</form>
//JS
// JS
function sendForm(){
model_data = $("#frm").serializeObject();
$.ajax({
url: 'YOUR_SERVICE_URL',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(model_data),
dataType: 'json',
success:function(e){
// I know, you do not want Ajax, if you callback to page, you can refresh page here
}
});
Good luck!
祝你好运!
#2
1
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?
您可以使用JSON.stringify()来序列化您的客户端对象,然后将其填充到隐藏的输入中并提交您的表单...然后在控制器端将其拉出Request.Form并将其反序列化为您的对象?
[Edit] Just saw in the comment section underneath of the original question that this was essentially a duplicate post and that this * worked as a solution.
[编辑]刚看到原始问题下面的评论部分,这实际上是一个重复的帖子,这个*作为一个解决方案。
#3
0
you can try;
你可以试试;
// html
// html
<form type="POST" action="/Home/Test">
<input id="foo" value="hede">
</form>
// dto
// dto
public class TestInDto{
public string foo {get;set;}
}
// home controller
//家庭控制器
[HttpPost]
void Test(TestInDto inDto){
var foo = inDto.foo;
}
#4
0
As per W3C standards you can't pass the data like JSON using
根据W3C标准,您无法使用JSON等数据传递
<form enctype="application/json"></form>
Description
描述
User agents that implement this specification
will transmit JSON
data from their forms whenever the form's enctype
attribute is set to application/json
. During the transition period, user agents that do not support this encoding will fall back
to using application/x-www-form-urlencoded
. This can be detected
on the server side, and the conversion algorithm
described in this specification
can be used to convert such data to JSON
.
只要表单的enctype属性设置为application / json,实现此规范的用户代理就会从其表单传输JSON数据。在过渡期间,不支持此编码的用户代理将回退到使用application / x-www-form-urlencoded。这可以在服务器端检测到,并且本规范中描述的转换算法可用于将这些数据转换为JSON。
The path format used in input names
is straightforward. To begin with, when no structuring information is present, the information will simply be captured as keys
in a JSON
object
输入名称中使用的路径格式很简单。首先,当没有结构化信息时,信息将被简单地捕获为JSON对象中的键
参考DOC
#5
0
You can now set form enctype='application/json' according to the new W3C standards published on 29 May 2014.
您现在可以根据2014年5月29日发布的新W3C标准设置表单enctype ='application / json'。
You can check it : http://www.w3.org/TR/html-json-forms/
您可以查看:http://www.w3.org/TR/html-json-forms/
#6
0
You can use Form2js.It is designed by google and it is easy to use library.
你可以使用Form2js.It是由谷歌设计的,它很容易使用库。
https://github.com/maxatwork/form2js
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:
此外,它可以根据用户要求进行修改。您可以查看他们的许可证。您可以使用以下链接查找此javascript文件的基本示例:
http://form2js.googlecode.com/hg/example/test.html
http://form2js.googlecode.com/hg/example/test.html
#7
-1
You can send the request by AJAX and then generate a file downloading URL in the controller and send it back in the AJAX response. Then you can just open up that URL in another window. This would be a complete AJAX-based solution so you can use JSON.
您可以通过AJAX发送请求,然后在控制器中生成文件下载URL并将其发送回AJAX响应中。然后,您可以在另一个窗口中打开该URL。这将是一个完整的基于AJAX的解决方案,因此您可以使用JSON。
#8
-1
Try this simple store your POST array in variable and then encode it as json obj. like this-->
尝试将这个简单的POST数组存储在变量中,然后将其编码为json obj。像这样 - >
$postarray=($_POST);
$jsondata=json_encode($postarray);$ postarray =($ _ POST); $ jsondata = json_encode($ postarray);
Sorry its for PHP
对不起它的PHP
#1
1
Yes, you can serialize form like an object with plugin. I write a sample for you;
是的,您可以使用插件将表单序列化为对象。我为你写了一个样本;
//Head
//头
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery.serialize-object.js"></script>
You can download plugin from here
你可以从这里下载插件
//Form
//形成
<form id="frm">
<input type="text" name="Model[Firstname]">
<input type="text" name="Model[Lastname]">
<input type="text" name="ModelDetail[PhoneNumber]">
...
<button type="button" onclick="sendForm()">Send</button>
</form>
//JS
// JS
function sendForm(){
model_data = $("#frm").serializeObject();
$.ajax({
url: 'YOUR_SERVICE_URL',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(model_data),
dataType: 'json',
success:function(e){
// I know, you do not want Ajax, if you callback to page, you can refresh page here
}
});
Good luck!
祝你好运!
#2
1
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?
您可以使用JSON.stringify()来序列化您的客户端对象,然后将其填充到隐藏的输入中并提交您的表单...然后在控制器端将其拉出Request.Form并将其反序列化为您的对象?
[Edit] Just saw in the comment section underneath of the original question that this was essentially a duplicate post and that this * worked as a solution.
[编辑]刚看到原始问题下面的评论部分,这实际上是一个重复的帖子,这个*作为一个解决方案。
#3
0
you can try;
你可以试试;
// html
// html
<form type="POST" action="/Home/Test">
<input id="foo" value="hede">
</form>
// dto
// dto
public class TestInDto{
public string foo {get;set;}
}
// home controller
//家庭控制器
[HttpPost]
void Test(TestInDto inDto){
var foo = inDto.foo;
}
#4
0
As per W3C standards you can't pass the data like JSON using
根据W3C标准,您无法使用JSON等数据传递
<form enctype="application/json"></form>
Description
描述
User agents that implement this specification
will transmit JSON
data from their forms whenever the form's enctype
attribute is set to application/json
. During the transition period, user agents that do not support this encoding will fall back
to using application/x-www-form-urlencoded
. This can be detected
on the server side, and the conversion algorithm
described in this specification
can be used to convert such data to JSON
.
只要表单的enctype属性设置为application / json,实现此规范的用户代理就会从其表单传输JSON数据。在过渡期间,不支持此编码的用户代理将回退到使用application / x-www-form-urlencoded。这可以在服务器端检测到,并且本规范中描述的转换算法可用于将这些数据转换为JSON。
The path format used in input names
is straightforward. To begin with, when no structuring information is present, the information will simply be captured as keys
in a JSON
object
输入名称中使用的路径格式很简单。首先,当没有结构化信息时,信息将被简单地捕获为JSON对象中的键
参考DOC
#5
0
You can now set form enctype='application/json' according to the new W3C standards published on 29 May 2014.
您现在可以根据2014年5月29日发布的新W3C标准设置表单enctype ='application / json'。
You can check it : http://www.w3.org/TR/html-json-forms/
您可以查看:http://www.w3.org/TR/html-json-forms/
#6
0
You can use Form2js.It is designed by google and it is easy to use library.
你可以使用Form2js.It是由谷歌设计的,它很容易使用库。
https://github.com/maxatwork/form2js
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:
此外,它可以根据用户要求进行修改。您可以查看他们的许可证。您可以使用以下链接查找此javascript文件的基本示例:
http://form2js.googlecode.com/hg/example/test.html
http://form2js.googlecode.com/hg/example/test.html
#7
-1
You can send the request by AJAX and then generate a file downloading URL in the controller and send it back in the AJAX response. Then you can just open up that URL in another window. This would be a complete AJAX-based solution so you can use JSON.
您可以通过AJAX发送请求,然后在控制器中生成文件下载URL并将其发送回AJAX响应中。然后,您可以在另一个窗口中打开该URL。这将是一个完整的基于AJAX的解决方案,因此您可以使用JSON。
#8
-1
Try this simple store your POST array in variable and then encode it as json obj. like this-->
尝试将这个简单的POST数组存储在变量中,然后将其编码为json obj。像这样 - >
$postarray=($_POST);
$jsondata=json_encode($postarray);$ postarray =($ _ POST); $ jsondata = json_encode($ postarray);
Sorry its for PHP
对不起它的PHP