axios发送请求的默认请求头方式
最近遇到一个问题就是使用axios的post发送请求添加数据默认请求头是 Cont-Type:aapplication/json;charset=utf-8 这属于正常 ,可是后来在编辑的时候用了put请求方式然后就报错,和后台对照发现那边请求头信息是appliction/x-www-form-urlencoded;chrset=utf-8 然后很头大 怎么办呢 好歹后来解决了
let formData = ''
if (this.classAddCompileVal === '新建班级') {
formData = new FormData() //这个请求头默认是 Cont-Type:aapplication/json;charset=utf-8
} else if (this.classAddCompileVal === '编辑班级') {
formData = new URLSearchParams() // 这个请求头默认是appliction/x-www-form-urlencoded;chrset=utf-8
}
formData.append('name', this.classInfo.className) // 班级名称
formData.append('year', this.classInfo.classVal) // 级的值
formData.append('grade', this.stageVal) // 阶段的值
this.$axios.put(`classes/${this.classID}`, formData).then(res => {
console.log(res)
if (res.status === 200) {
this.$message({
message: '编辑班级成功',
type: 'success'
})
this.$emit('close')
this.classInfo.className = ''
this.classInfo.classVal = ''
this.stageVal = ''
this.classInfo.belong = ''
this.teacher = ''
this.teachID = ''
this.classID = ''
}
}).catch(error => {
this.$message.error('该班级名称已存在!')
console.log(error)
})
浙公网安备 33010602011771号