Submitting form data in JSON-encoded format is HTML5 's further contribution to the evolution of web development, where our HTML form data is a server-side transmitted via Key-value, a form of transmission that is very primitive in its lack of management of the data organization. The new JSON format submits the form data method, converts all the data in the form to a certain canonical JSON format, and then transmits the server side. The data received on the server side is a qualified JSON code that can be used directly. How to declare a form to be submitted in JSON format
You should be familiar with how to use a form to upload a file, it needs to add a enctype= "Multipart/form-data" statement to the HTML form tag, which tells the browser to send the form data in the upload file mode. The JSON format submission form is similar to the one in which it is written: enctype= ' Application/json '.
Compatibility with older browsers
Submitting forms in JSON format a very new specification in HTML5, only a modern browser that implements these specifications can recognize the semantics of enctype= ' Application/json ' in order to properly package the form data into JSON format. For some older browsers, and browsers that have not yet implemented these standards, they do not recognize what enctype= ' Application/json ' represents, so the form's enctype automatically degenerates to application/ x-www-form-urlencoded the default encoding format. The server-side code can determine how to receive data based on the value of the enctype.
Sample format for submitting forms in JSON encoded format
Example 1 Basic usage
xml/html Code copy content to clipboard
- <form enctype= ' Application/json ' >
- <input name= ' name ' value= ' Bender ' >
- <select name= ' hind ' >
- <option selected>bitable</option>
- <option>Kickable</option>
- </select>
- <input type= ' checkbox ' name= ' shiny ' checked>
- </form>
- The generated JSON data is
- {
- ' Name ': ' Bender '
- , "Hind": "Bitable"
- , "Shiny": True
- }
Example 2 encoding by JSON array when there are multiple form fields with duplicate names in the form
xml/html Code copy content to clipboard
- <form enctype= ' Application/json ' >
- <input type= ' number ' name= ' bottle-on-wall ' value= ' 1 ' >
- <input type= ' number ' name= ' Bottle-on-wall ' value= ' 2 ' >
- <input type= ' number ' name= ' Bottle-on-wall ' value= ' 3 ' >
- </form>
- The generated JSON data is
- {
- "Bottle-on-wall": [1, 2, 3]
- }
Example 3 form domain name to form a complex structure that appears in an array
xml/html code to copy content to clipboard
- <form enctype= ' Application/json ' >
- <input name= ' pet[species] ' value= ' Dahut ' >
- <input name= ' pet[name] ' value= ' Hypatia ' >
- <input name= ' kids[1] ' value= ' Thelma ' >
- <input name= ' kids[0] ' value= ' Ashley ' >
- </form>
- The generated JSON data is
- {
- "Pet": {
- "Species": "Dahut"
- , "name": "Hypatia"
- }
- , "Kids": ["Ashley", "Thelma"]
- }
Example 4 in the above example, the missing array ordinal value is substituted with null
xml/html code to copy content to clipboard
- <form enctype= ' Application/json ' >
- <input name= ' hearbeat[0] ' value= ' thunk ' >
- <input name= ' hearbeat[2] ' value= ' thunk ' >
- </form>
- The generated JSON data is
- {
- "Hearbeat": ["thunk", NULL, "Thunk"]
- }
Example 5 multi-array nested format with unlimited number of nesting layers
xml/html Code copy content to clipboard
- <form enctype= ' Application/json ' >
- <input name= ' pet[0][species] ' value= ' Dahut ' >
- <input name= ' pet[0][name] ' value= ' Hypatia ' >
- <input name= ' pet[1][species] ' value= ' Felis stultus ' >
- <input name= ' pet[1][name] ' value= ' Billie ' >
- </form>
- The generated JSON data is
- {
- "Pet": [
- {
- "Species": "Dahut"
- , "name": "Hypatia"
- }
- , {
- "Species": "Felis stultus"
- , "name": "Billie"
- }
- ]
- }
Example 6 really, there is no array dimension limit!
xml/html Code copy content to clipboard
- <form enctype= ' Application/json ' >
- <input name= ' wow[such][deep][3][much][power][!] ' value= ' amaze ' >
- </form>
- The generated JSON data is
- {
- "Wow": {
- "Such": {
- "Deep": [
- Null
- Null
- Null
- , {
- "Much": {
- "Power": {
- "!": "Amaze"
- }
- }
- }
- ]
- }
- }
- }
Example 7 file Upload
xml/html Code copy content to clipboard
- <form enctype= ' Application/json '
- <input type= ' file ' name= ' file ' Multiple>
- & L T;/form>
-
- //Assuming you uploaded 2 files, the generated JSON data is:
- {
- "file": [
- {
]
- ' type ': ' Text/plain ',
- ' name ': ' Dahut.txt ',
- "Body": "refbqufbqufivvvvvvvvvvvvvcehiqo="
- },
- {
- ' type ': ' Text/plain ',
- ' name ': ' Litany.txt ',
- ' body ': ssbtdxn0ig5vdcbm zwfyllxurmvhcibpcyb0agugbwluzc1rawxszxiucg== "
- }
- ]
- }