HTML5 JSON格式提交表单

HTML5可以实现以JSON格式提交表单

1.基本用法

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

  生成的JSON数据是
     {
          "name":"Bender",
          "hind":"Bitable",
          "shiny":true
     }
2.当表单存在多个重名的表单域时,按JSON数据组编码
<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>
生成的JSON数据是
     {
          "bottle-on-wall":[1,2,3]
     }
3.表单域名称以数组形式出现的复杂结构 
<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>

 生成的JSON数据是
     {
          "pet":{
               "species":"Dahut",
               "name":"Hypatia"
          },
          "kids":["Ashley","Thelma"]
     }
4.在上面的例子中,缺失的数组序号值将以null替代
<form enctype='application/json'>
          <input name='hearbeat[0]' value='thunk'>
          <input name='hearbeat[2]' value='thunk'>
</form>
生成的JSON数据是
     {
          "hearbeat":["thunk",null,"thunk"]
     }
5.多重数组嵌套格式,嵌套层数无限制
<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>
 生成的JSON数据是
     {
          "pet":[
          {
               "species":"Dahut",
               "name":"Hypatia"
          },
          {
               "species":"Felis Stultus",
               "name":"Billie"
          }
          ]
     }
6.没有数组维度限制
<form enctype='application/json'>
          <input name='wow[such][deep][3][much][power][!]' value='Amaze'>
</form>
 生成的JSON数据是
     {
          "wow":{
               "such":{
                    "deep":[
                         null,
                         null,
                         null,
                         {
                              "much":{
                                   "power":{
                                        "!":"Amaze"
                                   }
                              }
                         }
                    ]
               }
          }
     }
7.文件上传
<form enctype='application/json'>
          <input type='file' name='file' multipart>
</form>
  假设上传了两个文件,生成的JSON数据是
     {
          "file":[
          {
               "type":"text/plain",
               "name":"dahut.txt",
               "body":"xxxxxxxxxx"
          },
          {
               "type":"text/plain",
               "name":"litany.txt",
               "body":"xxxxxxxxx"
          }
          ]
     }







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值