is form enctype application/json available?
The W3C publishes many drafts and proposals which are then discussed within the community at large. If a draft makes it to the stage where it's generally considered useful, browser vendors will/may start implementing it. The draft then typically advances to a "recommendation" stage, meaning the W3C officially recommends that browsers implement the technology as specified; but of course they can't twist anyone's arm to actually do so.
Each document will say at its top what its current status is, and http://www.w3.org/TR/ lists all current documents and their status. The one you picked is listed as "obsolete" and "retired" on that page and has a ginormous banner at its top saying:
Beware. This specification is no longer in active maintenance and the HTML Working Group does not intend to maintain it further.
So, no, probably no browser is currently implementing it.
To track the real-world availability of a feature you need to look to 3rd party resources like https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-enctype and http://caniuse.com.
How to catch data from form enctype=json
try this:
console.log(JSON.stringify(data));
How to send a JSON object using html form data
Get complete form data as array and json stringify it.
var formData = JSON.stringify($("#myForm").serializeArray());
You can use it later in ajax. Or if you are not using ajax; put it in hidden textarea and pass to server. If this data is passed as json string via normal form data then you have to decode it. You'll then get all data in an array.
$.ajax({
type: "POST",
url: "serverUrl",
data: formData,
success: function(){},
dataType: "json",
contentType : "application/json"
});
Set enctype attribute to application/json
Does anyone have ideas about how to make a POST request and use JSON instead of formdata as the data format without resorting to AJAX?
There is no way to do this. Work on JSON as a form encoding type has been abandoned.
If you are writing an HTTP endpoint that expects normal form submissions, write it so it accepts application/x-www-form-urlencoded
and multipart/form-data
encoded data.
Those are the only encoding types that browsers support for form submissions (other than text/plain
which isn't suitable for machine processing).
The only way to send a JSON payload is with Ajax, which you rejected as an option.
Specify Content-Type in form-data part as application/json
This might work too.
.addFormDataPart(
"config",
null,
RequestBody.create(MediaType.parse("application/json"), "{\"access\":\"YES\"}")
)
What are the differences between application/json and application/x-www-form-urlencoded?
The first case is telling the web server that you are posting JSON data as in:
{"Name": "John Smith", "Age": 23}
The second case is telling the web server that you will be encoding the parameters in the URL:
Name=John+Smith&Age=23
What does enctype='multipart/form-data' mean?
When you make a POST request, you have to encode the data that forms the body of the request in some way.
HTML forms provide three methods of encoding.
application/x-www-form-urlencoded
(the default)multipart/form-data
text/plain
Work was being done on adding application/json
, but that has been abandoned.
(Other encodings are possible with HTTP requests generated using other means than an HTML form submission. JSON is a common format for use with web services and some still use SOAP.)
The specifics of the formats don't matter to most developers. The important points are:
- Never use
text/plain
.
When you are writing client-side code:
- use
multipart/form-data
when your form includes any<input type="file">
elements - otherwise you can use
multipart/form-data
orapplication/x-www-form-urlencoded
butapplication/x-www-form-urlencoded
will be more efficient
When you are writing server-side code:
- Use a prewritten form handling library
Most (such as Perl's CGI->param
or the one exposed by PHP's $_POST
superglobal) will take care of the differences for you. Don't bother trying to parse the raw input received by the server.
Sometimes you will find a library that can't handle both formats. Node.js's most popular library for handling form data is body-parser which cannot handle multipart requests (but has documentation that recommends some alternatives which can).
If you are writing (or debugging) a library for parsing or generating the raw data, then you need to start worrying about the format. You might also want to know about it for interest's sake.
application/x-www-form-urlencoded
is more or less the same as a query string on the end of the URL.
multipart/form-data
is significantly more complicated but it allows entire files to be included in the data. An example of the result can be found in the HTML 4 specification.
text/plain
is introduced by HTML 5 and is useful only for debugging — from the spec: They are not reliably interpretable by computer — and I'd argue that the others combined with tools (like the Network Panel in the developer tools of most browsers) are better for that).
Submitting form and set content type to application/json in Laravel
Its PHP behavior to convert .
into _
if exist in post/get request
Note:
Dots and spaces in variable names are converted to underscores. For
example<input name="a.b" />
becomes$_REQUEST["a_b"]
.
Get more details in PHP Doc
In Template
<input name="user.email" value="john@abc.com">
In Controller
request()->input('user_name') // Will give you john@abc.com
To teat it as array use like
<input name="user[name]" value="John">
<input name="user[email]" value="john@abc.com">
Now you can use .
in controller
request()->input('user.name') // will give John
request()->input('user.email') // will give john@abc.com
Why would you use FormData over JSON in sending POST requests?
It depends on what the server is accepting, typically if you're are interacting with an API you will send over JSON, which informs the server about the type of data being sent over. If you send it via a form, the content-type
(in the header of the request), will be application/x-www-form-urlencoded
.
So the server needs to be equipped, typically in the form of some type of middleware in order to parse it. For example, in express js, you would have something like the following,
// used to parse json
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
Related Topics
CSS to Align Label and Inputs on Form
How to Make a Table with Equal Column Widths in CSS
HTML 5 Audio Tag Multiple Files
Why Does an Anchor Tag's Href Values Need Http:// Preprended to The Url
How to Wrap My Markdown in an HTML Div
<Table> with Fixed <Thead> and Scrollable <Tbody>
How to Set Favicon.Ico Properly on Vue.Js Webpack Project
Why Are Bootstrap's Form Elements Rendered Terribly with Struts2-Boostrap-Plugin
Use Multiple @Font-Face Rules in CSS
Streaming a Video from Google Drive Using HTML5 Video Tag
Remove .HTML and .PHP Extensions with .Htaccess
Play Audio Local File with HTML
How to Make a Div with Arrowlike Side Without CSS Border Tricks
Use Excel Vba to Click on a Button in Internet Explorer, When The Button Has No "Name" Associated