vuejs使用axios异步访问时用get和post的实例讲解
script中。
letdata={....};
leturl=xx;
方法各异:
GET:
this.$ajax.get(url,{
params:data
})
.then(function(res){
console.log(res.data)
})
.catch(function(error){
console.log(error)
})
POST:
方法一:1.
//请求后台数据之前转换入参
leturl=xx;
letdata={
uname:this.uname,
upwd:this.upwd,
pid:this.pid
};
functiontransformRequest(data){
letret=''
for(letitindata){
ret+=encodeURIComponent(it)+'='+encodeURIComponent(data[it])+'&'
}
returnret
}
letp=transformRequest(data)
this.$ajax.post(url,p)
.then(function(res){
console.log(res.data)
})
.catch(function(error){
console.log(error)
})
方法二:2
varqs=require('qs');
this.$ajax.get(url,qs.stringfy(data))
.then(function(res){
console.log(res.data)
})
.catch(function(error){
console.log(error)
})
this.$ajax是注入axios通过修改vue原型重新命名的,还是axios。
以上这篇vuejs使用axios异步访问时用get和post的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。