开发者

Submit a form as JSON (no AJAX)

开发者 https://www.devze.com 2023-04-11 01:46 出处:网络
Is it possible to submit form data as JSON, without using AJAX? I\'ve tried changing the enctype: <form enctype=\"application/json\"></form>

Is it possible to submit form data as JSON, without using AJAX?

I've tried changing the enctype:

<form enctype="application/json"></form>
开发者_如何学C

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.


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!


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.


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;

}


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


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/


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消