vue项目中调用接口formdata格式传参写法
作者:chrispy
axios调用接口,默认传参形式为json
传参形式一(formdata):上传文件也可使用此方式
loginSubmit: function (){
var _this=this;
//formdata格式传数据
var formdata = new FormData();
formdata.append('userName', this.userName);
formdata.append('password', this.password);
formdata.append('userCode', this.regCode);
this.$ajax({
url:'/IntelligentReminderSystem/login/signIn',
method:'post',
data:formdata
}).then(function(res){
console.log(res)
var result=res.data
if(result.success){
_this.$router.push('home')
}else{
alert(result.message)
_this.getCode();
}
})
}
传参形式二(formdata):
loginSubmit: function (){
var _this = this
var obj = {}
obj.userName = this.userName
obj.password = this.password
obj.userCode = this.regCode
this.$ajax({
url:'/IntelligentReminderSystem/login/signIn',
method:'post',
data:obj,
transformRequest: [function (data) {
let ret = ''
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
return ret
}]
}).then(function(res){
console.log(res)
var result = res.data
if(result.success){
_this.$router.push('home')
}else{
alert(result.message)
_this.getCode();
}
})
}
传参形式三(json):axios默认传参形式
loginSubmit: function (){
var _this = this;
var obj = {}
obj.userName = this.userName
obj.password = this.password
obj.userCode = this.regCode
this.$ajax({
url:'/IntelligentReminderSystem/login/signIn',
method:'post',
data:obj
}).then(function(res){
console.log(res)
var result = res.data
if(result.success){
_this.$router.push('home')
}else{
alert(result.message)
_this.getCode();
}
})
}