今天在学习VUE时遇到了一个关于请求传参的问题,百思不得其解,记录一下
环境
后端用的是node.js
前端用的是vue框架
问题代码
addParams () {
this.$refs.addFormRef.validate(async vaild => {
if (!vaild) return
//问题代码
const { data: res } = await this.$http({
url: `categories/${this.catId}/attributes`,
method: 'POST',
params: { attr_sel: this.activeName, attr_name: this.addForm.attr_name }
})
//
if (res.meta.status !== 201) { return this.$message.error('添加参数失败') }
this.$message.success('添加参数成功')
this.addDialogVisible = false
this.getParamsData()
console.log(res)
})
}
addParams () {
this.$refs.addFormRef.validate(async vaild => {
if (!vaild) return
const { data: res } = await this.$http.post(`categories/${this.catId}/attributes`,
// 参数
{ params: { attr_sel: this.activeName, attr_name: this.addForm.attr_name } })
if (res.meta.status !== 201) { return this.$message.error('添加参数失败') }
this.$message.success('添加参数成功')
this.addDialogVisible = false
this.getParamsData()
console.log(res)
})
}
没有问题的代码
addParams () {
this.$refs.addFormRef.validate(async vaild => {
if (!vaild) return
const { data: res } = await this.$http.post(`categories/${this.catId}/attributes`,
// 参数
{ attr_sel: this.activeName, attr_name: this.addForm.attr_name })
if (res.meta.status !== 201) { return this.$message.error('添加参数失败') }
this.$message.success('添加参数成功')
this.addDialogVisible = false
this.getParamsData()
console.log(res)
})
}
问题描述
当axios用post请求时,参数用params传就拿不到后端的数据,而在get请求里用params却没有问题。
问题思考
在网上和axios的文档上看了看相关的格式要求,post是如下样子
this.$axios({
method:"post",
url:"/api/",
data:{
name:this.name,
password:this.password
}
}).then((res)=>{
console.log(res.data);
})
等同于
this.$http.post(`categories/${this.catId}/attributes`,
// 参数
{
attr_sel: this.activeName,
attr_name: this.addForm.attr_name
})
在网上找了找相关的回答,
params是添加到url的请求字符bai串中的,用于get请求
而data是添加到请求体(body)中的, 用于post请求
目前没有找到在post请求里用params传参的方法,记录一下以便继续学习
1239



被折叠的 条评论
为什么被折叠?



