ajax模仿form上传图片
<form id="iconForm"> <input class="js_upFile cover1" type="file" name="img" id="img" @change="getPicFile" style="display:none;"/> <input type="text" name="user_id" v-model="userId" style="display:none;"/> <input type="text" name="img_url" v-model="headPortraitB" style="display:none;"/> </form>
// 上传图片给后端 // 三个元素都在form表单里了,name对应key let form = document.getElementById("iconForm") let formData = new FormData(form) $.ajax({ url: urlhost+"usercenter/postavatar", type: "post", data: formData, processData:false, // 这两项是配合formData的,不是formData禁止使用!否则后台拿不到data contentType:false, success:function(data){ let mess = JSON.parse(data) console.log('data', data); }, error:function(e){ console.log(e); } })