需求:有一个接口是,post请求,请求头 content-Type:"application/json",请求参数类型有body和query两种。
1.首先封装API接口:
export async function addRoom(query, data){
return await axios({
url:'/.....',
method:"post",
params: query,
paramsSerializer: function (query) {
return Qs.stringify(query, { arrayFormat: 'repeat' })
},
data: data,
})
}
2. 在请求拦截器设置请求头
// request拦截器
service.interceptors.request.use(
config => {
....
config.headers["content-type"] =
"application/json;";
return config;
},
error => {
// Do something with request error
Promise.reject(error);
}
);
3. 调用API传参
const { code,message } = await addRoom(
this.form,
this.form.idLists
);
搞定了!!