FormData()简单使用

xiaoxiao2021-02-28  66

通过FormData对象可以组装一组用 XMLHttpRequest发送请求的键值对。它可以更灵活方便的发送表单数据,因为可以独立于表单使用。 如果你把表单的编码类型设置为multipart/form-data ,则通过FormData传输的数据格式和表单通过submit() 方法传输的数据格式相同 通过FormData()我们可以不依赖from表单上传文件给后端 // 保存用户 saveUser () { let that = this var data = new FormData() data.append('avatar', that.imgDataUrl) data.append('id', that.userForm.id) data.append('role_id', that.userForm.role_id) data.append('name', that.userForm.name) data.append('email', that.userForm.email) data.append('password', that.userForm.password) data.append('password_confirmation', that.userForm.password) //data添加过字段以后,打印是看不到的 //封装的axios插件 that句柄 请求的接口 发送的参数 回调函数 that.post(that, 'employeesAdd', data, function (res) { if (res.success) { that.success(that, '保存成功!') that.goBack() } else { if (res.code === '203') { that.userForm.nameExist = that.userForm.email that.saveUser() return false } that.error(that, res.msg) } }) }
转载请注明原文地址: https://www.6miu.com/read-1000299.html

最新回复(0)