下面内容是关于 Query String Parameters
、Form Data
、 Request Payload
三种格式的区别。主要是由于 Content-Type
与请求方式 method
不一样,致使传递的数据格式不一样。html
原生 ajax 请求默认 Content-Type: text/plain;charset=UTF-8
git
常见的媒体格式:github
*1. Query String Parameters
格式: ?key=value&key=value
web
参数会以 url string 的形式进行传递,即?后的字符串则为其请求参数,并以&做为分隔符。经常使用在 GET 请求方式时使用。 其余请求方式也可使用,拼接在接口地址 `url?` 后面。 
*2. Form Data
格式:key=value&key=value
键值对形式ajax
Content-type
为 application/x-www-form-urlencoded;charset=utf-8
时,参数会以 Form Data
的形式(数据为 String 键值对格式)传递给接口,而且不会显示在接口 url 上。let data = { username: 'wqjiao', password: '111111', } xhr.send(QS.stringify(data));
new FormData()
方法处理后传递给接口,Content-type
为 multipart/form-data; boundary=----WebKitFormBoundarys9jOoKcA1Kwn9sYS
格式。const formData = new FormData(); formData.append('label', 'ID_photo-front'); formData.append('subId', 'fa6abb94000a4ba1b19a43e60eba1516'); formData.append('file', fileList[0]); fetch('http://192.168.1.128:5022/tool/file/upload', { method: 'POST', headers: { 'Authorization': '89199cf3294520765904d47c2c570c1b', }, body: formData, cridentials: 'include', mode: 'no-cors', processData: false, cache: false, contentType: false, });
补充说明json
服务器为何会对表单提交和文件上传作特殊处理,由于表单提交数据是名值对的方式,且Content-Type为application/x-www-form-urlencoded,而文件上传服务器须要特殊处理,普通的post请求(Content-Type不是application/x-www-form-urlencoded)数据格式不固定,不必定是名值对的方式,因此服务器没法知道具体的处理方式,因此只能经过获取原始数据流的方式来进行解析。
formdata
对象,不须要对数据作处理。<form>
表单构造的 FormData
对象,且已经声明了属性 enctype="multipart/form-data"
,因此这里设置为 false。*3. Request Payload
格式:{key: value, key: value}
(后端通过反序列化获得对象)后端
当 `Content-type` 为 `application/json;charset=utf-8` 时,参数会以 `Request Payload` 的形式(数据为 json 格式)传递给接口,而且不会显示在接口 url 上。 ```js let data = { username: 'wqjiao', password: '111111', } xhr.send(Qs.stringify(data)); ```
let Ajax = { get: function (url, fn) { // XMLHttpRequest对象用于在后台与服务器交换数据 let xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { // readyState == 4说明请求已完成 if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) { // 从服务器得到数据 fn.call(this, xhr.responseText); } }; xhr.send(); }, // data 应为'a=a1&b=b1'这种字符串格式,在jq里若是data为对象会自动将对象转成这种字符串格式 post: function (url, data, fn) { let xhr = new XMLHttpRequest(); xhr.open("POST", url, true); // xhr.open("POST", '/response.json', true); xhr.setRequestHeader('Accept', 'application/json') xhr.setRequestHeader('Authorization', 'bb850ec168d55eedcb0b47ac4e7c9d6b') // 添加http头,发送信息至服务器时内容编码类型 xhr.setRequestHeader("Content-Type", "application/json"); // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) { fn.call(this, xhr.responseText); } }; // application/json 格式:{key: value, key: value} xhr.send(JSON.stringify(data)); // application/x-www-form-urlencoded 格式:key=value&key=value // xhr.send(Qs.stringify(data)); } }
获取数据跨域
formData.get('name');
formData.getAll('username');
formData.append('username', 'qq');
formData.set("username", 'wqjiao');
注意:若是 formData
中不存在 username 字段,那么,js不会报错,而是直接新增当前键值对。数组
formData.has('username');
formData.delete('username');
formData.entries()
formData.entries()
方法返回一个 iterator 对象 ,此对象能够遍历访问 formData
中的键值对。其中键值对的 key 是一个 USVString 对象;value 是一个 USVString , 或者 Blob 对象。缓存
实例
<form id="myForm"> <input type="text" name="name" placeholder="请输入你的名字" /> <input type="password" name="password" placeholder="请输入你的密码" /> <input type="text" name="n1" /> <input type="text" name="n1" /> <input type="submit" id="submitBtn" value="提交" /> </form> <script> // 表单初始化 let form = document.getElementById('myForm'); let submitBtn = document.getElementById('submitBtn'); submitBtn.addEventListener('click', function (e) { e.preventDefault(); let formData = new FormData(form); // 获取数据 let name = formData.get('name'); let arr = formData.getAll('n1'); // 添加数据 formData.append('username', 'wqjiao'); // 判断是否存在 name 为 deletename if (formData.has('deletename')) { // 修改数据 formData.set("deletename", '123456'); } // 删除数据 formData.delete('password'); // 返回全部的数据,遇到重复的 name,保留最新的值 let data = {}; for(let name of formData.entries()) { if (name) { data[name[0]] = name[1]; } } console.log(name, arr, data); // 请求接口 let xhr = new XMLHttpRequest(); xhr.open('POST', 'response.json', true); xhr.send(formData); }, false); </script>