vue2上傳圖片,使用axios發(fā)送FormData數(shù)據(jù),參數(shù)為空
2018.08.20 21:52字數(shù) 148閱讀 5669評論 2喜歡 10
最近在做項目時,需要用到上傳圖片的功能,用axios上傳圖片,用FormData發(fā)送數(shù)據(jù),參數(shù)一直為空,如圖顯示

1.jpg

2.png

3.png
查找并嘗試解決辦法,在請求中,添加
headers:{
'Content-Type':'multipart/form-data'
}
依然無效。。。
最終一個朋友幫我get到問題點了,是需要設(shè)置 withCredentials:true? , 表示跨域請求時是否需要使用憑證. 默認為false
完整代碼如下
authen(event) {? ? ? ? event.preventDefault();letformData =newFormData();? ? ? ? ? formData.append('fileUpload1',this.files);? ? ? ? ? formData.append('fileUpload2',this.files2);? ? ? ? ? formData.append('fileUpload3',this.files3);? ? ? ? ? formData.append('uid',this.userID);? ? ? ? formData.append('name',this.name);constinstance=axios.create({withCredentials:true})? ? ? ? ? ? ? ? ? ? ? instance.post('http://pay.m6zf.com/index.php?s=/api/user/approved.html',formData).then(res=>{if(res.code ==200){? ? ? ? ? ? ? ? ? ? alert('提交成功');this.$router.push({path:"/Profit"});? ? ? ? ? ? ? ? }else{? ? ? ? ? ? ? ? ? ? alert("請輸入完整再提交");? ? ? ? ? ? ? ? }? ? ? ? ? ? })? ? ? ? }
將withCredentials:true設(shè)置后,提交請求,請求參數(shù)終于有數(shù)據(jù)了

success1.jpg

success2.jpg
小禮物走一走,來簡書關(guān)注我