做项目的时候,之前都是单个单个参数传递到spring boot后端,后端能直接以对象形式接收,但是前端这样一个个写太麻烦了,同学就写了这样的一个方法给我看,记录一下
(有些axios的post方法传到后端就接收不了,比如上面注释掉的部分直接传对象就接收不到,但是单个个传又没问题,下面没注释的传对象就能直接接收)
data数据
form: {
companyId: '',
address: '',
eventType: '',
occurredTime: '',
description: '',
reason1: '',
reason2: '',
reason3: '',
measure: ''
}
提交方法
onSubmit() {
let data = this.form;
console.log(data);
// this.$axios
// .post(
// this.$GLOBAL.baseUrl + '/accidentAdd',
// this.$qs.stringify({
// // companyId: data.companyId,
// // address: data.address,
// // eventType: data.eventType,
// // occurredTime: data.occurredTime,
// // description: data.description,
// // reason1: data.reason1,
// // reason2: data.reason2,
// // reason3: data.reason3,
// // measure: data.measure
// accident: data
// })
// )
// .then((res) => {
// this.$message.success('提交成功!');
// bus.$emit('close_current_tags');
// });
this.$axios({
method: 'post',
url: this.$GLOBAL.baseUrl + '/accidentAdd',
data: data,
transformRequest: [
(data) => {
return this.$qs.stringify(data); //使用Qs将请求参数序列化
}
]
}).then((res) => {
//逻辑代码
console.log(res);
});
}