1 2 3 4 5
| get(‘key’) 获取表单对象属性的值 set(‘key’, ‘value’) 设置表单对象属性的值 delete(‘key’) 删除表单对象属性中的值 append(key,value) 在数据末尾追加数据 getAll(‘user’)获取取key为user的所有值 [‘a’,‘b’]
|
1 2
| 将form表单元素的name与value进行组合,实现表单数据的序列化 异步上传二进制文件
|
3. 在 vue 中使用 fromData 格式传参
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 首先要创建fromData实例 然后将传入的参数添加到实例中 最后请求接口,将参数传入 例如: vant组件 上传头像
<van-uploader :after-read="toImg"> <div>拍照</div> </van-uploader>
toImg(item) { let data = new FormData(); data.append("file", item.file); this.$API.imgs(data).then((res) => { console.log(res); }); }); },
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| 原生写法 上传
<form id="form"> <input type="text" name="username"> <input type="password" name="password"> <input type="button" id="btn" value="提交"> </form> <script type="text/javascript"> var btn = document.getElementById('btn'); var form = document.getElementById('form'); btn.onclick = function () { var formData = new FormData(form); var xhr = new XMLHttpRequest(); xhr.open('post', 'http://localhost:3000/formData'); xhr.send(formData); xhr.onload = function () { if (xhr.status == 200) { console.log(xhr.responseText); } } } </script>
|