|
问题:vue中使用axios 发现post 请求,后台获取不了参数
代码如下: this.$axios.post(this.$api.user.UserLogin,
{
userName:this.username,
userPassword:this.pwd
},{
headers : {
'Content-Type':'application/x-www-form-urlencoded'
}
}).then((res)=>{
if(res.data.returnCode == 200){
// 登录成功
}else{
alert(res.data.message);
}
})
}解决方案: axios 中的post方法,不能直接传递对象,要将对象转成字符串,在进行传递,两种方法 1. transformRequest方式,自己写将object 转成字符串的方法 this.$axios.post(this.$api.user.UserLogin,
{
userName:this.username,
userPassword:this.pwd
},{
transformRequest:[
function(data){
let params = "";
var arr = [];
for(var key in data){
arr.push(key+"="+data[key]);
}
params = arr.join("&");
return params;
}
] },
{
headers : {
'Content-Type':'application/x-www-form-urlencoded'
}
}).then((res)=>{
if(res.data.returnCode == 200){
// 登录成功
}else{
alert(res.data.message);
}
})
}2. 使用qs模块下载qs模块,使用命令 npm install qs -d 然后,使用qs模块 import qs from 'qs'
this.$axios.post(this.$api.user.UserLogin,qs.stringify({
userName:this.username,
userPassword:this.pwd
}),{
headers : {
'Content-Type':'application/x-www-form-urlencoded'
}
}).then((res)=>{
if(res.data.returnCode == 200){
// 登录成功
}else{
alert(res.data.message);
}
})
}参考 :http://bugshouji.com/bbs-read-run?tid=528 |
|
|