封装axios,发送的POST请求,三种方式

1.第一种文件格式。在入参数的时候new FormData(),然后把formData这个对象传入就OK

let formData = new FormData();
formData.append("file", param.file);

 

 2.就是普通的data,然后传入对象就OK

 let data = {
   phone:this.user.phone,
    realName:this.user.realName || '',
    password:this.user.password || ''
};

 

 3.第三种方式,传入字符串,这时候需要QS帮助把对象转成传入字符串,这个方法换成第一种方法一样是传入的

首先封装的请求中加入 headers: {"Content-Type": "application/x-www-form-urlencoded"}

export function addBatchUserApi(data) {
  return request({
    url: `/admin/user/batch/create`,
    method: 'post',
    data:data,
    headers: {"Content-Type": "application/x-www-form-urlencoded"}
  })
}

再在需要入参数的地方,需要这么处理,然后把qsdata传入请求中

import  qs from 'qs'
let data = {
    text:this.batchUser.text,
};
let qsdata=qs.stringify(data)

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章