In depth analysis of the JSON encoded form data submission

  • 2020-06-22 23:44:35
  • OfStack

Submission of form data in JSON encoding format is another great contribution of HTML5 to the development and evolution of WEB. Before, our HTML form data was transmitted through the server side of ES5en-ES6en mode, which lacks management of data organization and is 10 minutes original in form. The new JSON format submission method converts all the data in the form into JSON format with 1 specification, and then transfers to the server. The data received on the server side is qualified JSON code that can be used directly.

How do I declare a form submitted in JSON format

You should be familiar with how to upload a file using a form. It requires that enctype="multipart/ form-ES20en "be added to the form tag in HTML, which tells the browser to send the form data in upload mode. The JSON form submission declaration is similar, written as: enctype='application/json'.

Compatibility with older browsers

Submitting forms in JSON is one of the newer specifications in HTML5. Only modern browsers implementing these specifications can recognize the semantics of enctype='application/json' and properly package form data in JSON format. For older browsers and those that do not implement these standards, enctype='application/json' will not recognize what enctype='application/json' stands for, so the form's enctype will automatically degrade to the default encoding of application/ ES40en-ES41en-ES42en-ES43en. The server-side code can determine how to receive the data based on the value of enctype.

An example format of the JSON encoded format submission form

Example 1 basic usage


<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 The data is 
{
 "name":  "Bender"
, "hind":  "Bitable"
, "shiny": true
}

Example 2. When the form has multiple form fields with the same name, code as the JSON array


<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 The data is 
{
 "bottle-on-wall":  [1, 2, 3]
}

Example 3 is a complex structure that appears as an array of form domain names


<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 The data is 
{
  "pet": {
    "species": "Dahut"
  ,  "name":   "Hypatia"
  }
,  "kids":  ["Ashley", "Thelma"]
}

In the above example, the missing array ordinal value will be replaced by null


<form enctype='application/json'>
 <input name='hearbeat[0]' value='thunk'>
 <input name='hearbeat[2]' value='thunk'>
</form>

//  The generated Json The data is 
{
  "hearbeat":  ["thunk", null, "thunk"]
}

Example 5 Multiple array nesting format with unlimited nesting levels


<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 The data is 
{
 "pet": [
  {
   "species": "Dahut"
  , "name":  "Hypatia"
  }
 , {
   "species": "Felis Stultus"
  , "name":  "Billie"
  }
 ]
}

Example 6 Really, there is no array dimension limit!


<form enctype='application/json'>
 <input name='wow[such][deep][3][much][power][!]' value='Amaze'>
</form>

//  The generated Json The data is 
{
 "wow": {
  "such": {
   "deep": [
    null
   , null
   , null
   , {
     "much": {
      "power": {
       "!": "Amaze"
      }
     }
    }
   ]
  }
 }
}

Example 7 File upload


<form enctype='application/json'>
 <input type='file' name='file' multiple>
</form>

//  Let's say you upload it 2 A file ,  The generated Json The data is :
{
 "file": [
  {
   "type": "text/plain",
   "name": "dahut.txt",
   "body": "REFBQUFBQUFIVVVVVVVVVVVVVCEhIQo="
  },
  {
   "type": "text/plain",
   "name": "litany.txt",
   "body": "SSBtdXN0IG5vdCBmZWFyLlxuRmVhciBpcyB0aGUgbWluZC1raWxsZXIuCg=="
  }
 ]
}


Related articles: