以 JSON 形式提交表单(无 AJAX)

发布于 2024-12-07 22:04:38 字数 398 浏览 0 评论 0原文

是否可以在不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流心雨 2024-12-14 22:04:38

是的,您可以使用插件像对象一样序列化表单。我给你写了一个样本;

//Head

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery.serialize-object.js"></script>

您可以从此处下载插件

//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

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
   }
});

祝你好运!

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

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!

我一直都在从未离去 2024-12-14 22:04:38

您可以使用 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.

〃温暖了心ぐ 2024-12-14 22:04:38

你可以尝试;

// html

<form type="POST" action="/Home/Test">
<input id="foo" value="hede">
</form>

// dto

public class TestInDto{
 public string foo {get;set;}
}

// 主控制器

[HttpPost]
void Test(TestInDto inDto){
 var foo = inDto.foo;

}

you can try;

// html

<form type="POST" action="/Home/Test">
<input id="foo" value="hede">
</form>

// dto

public class TestInDto{
 public string foo {get;set;}
}

// home controller

[HttpPost]
void Test(TestInDto inDto){
 var foo = inDto.foo;

}
数理化全能战士 2024-12-14 22:04:38

根据 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 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.

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

Reference DOC

尹雨沫 2024-12-14 22:04:38

您现在可以根据 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/

如痴如狂 2024-12-14 22:04:38

你可以使用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

治碍 2024-12-14 22:04:38

尝试这个简单的方法,将您的 POST 数组存储在变量中,然后将其编码为 json obj。
像这样-->

$postarray=($_POST);
$jsondata=json_encode($postarray);

抱歉,它是 PHP 的

Try this simple store your POST array in variable and then encode it as json obj.
like this-->

$postarray=($_POST);
$jsondata=json_encode($postarray);

Sorry its for PHP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文