一、form-data格式
请求头:
二、json格式
请求头:
三、四种常见的 POST 提交数据方式
1.application/x-www-form-urlencoded
表单格式提交数据,通过form标签的action属性,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据
html:
<form id="myform" onsubmit="return false;">
用户名:<input type="text" id="username" name="username"/>
密码:<input type="password" id="psw" name="psw"/><br/>
<!-- 头像:<input type="file" name="head"></form> -->
<div class="button">
<button class="btn btn-primary" id="register">注册</button>
</div>
</form>
js:
jq:
首先,Content-Type 被指定为 application/x-www-form-urlencoded;其次,提交的数据按照 key1=val1&key2=val2 的方式进行编码,key 和 val 会进行URL 转码。
2.multipart/form-data(文件上传)
这也是一个常见的 POST 数据提交的方式。我们使用表单上传文件时,就要让 form 的 enctype 等于这个值。
首先生成了一个 boundary 用于分割不同的字段,为了避免与正文内容重复,boundary 很长很复杂。然后 Content-Type 里指明了数据是以 mutipart/form-data 来编码,本次请求的 boundary 是什么内容。消息主体里按照字段个数又分为多个结构类似的部分,每部分都是以 --boundary 开始,紧接着内容描述信息,然后是回车,最后是字段具体内容(文本或二进制)。如果传输的是文件,还要包含文件名和文件类型信息。消息主体最后以 --boundary-- 表示结束。
//js
document.getElementById("register")