【axios】get和post请求用法
【axios】get和post请求用法详解
·
一、get请求
1、语法:axios.get('url',{params:{参数名1:参数值1,参数名2:参数值2}}),then(res=>{this.参数名=res.data})
2、参数解析:
url:访问地址,host+路由,如:http://localhost:8000/add_apis/
params:参数,支持多参数,以英文逗号分隔。调取接口时,拼接在url后面。如:http://localhost:8000/add_apis/?project_id=1
this.参数名:指已在bom层声明并初始化的变量,如:apis:[],已在dom层中引用,接口返回数据后,则会渲染后展示。
3、实现代码
add_apis(){
axios.get('http://localhost:8000/add_apis/',{
params:{
project_id : this.project_id
}
}).then(res=>{
this.apis = res.data
})
},
二、post请求
1、语法:axios.post('url',this.参数名1{params:{参数名1:参数值1,参数名2:参数值2}}),then(res=>{this.参数名2=res.data})
2、参数解析
url:访问地址,host+路由,如:http://localhost:8000/save_api/
params:参数,支持多参数,以英文逗号分隔。调取接口时,拼接在url后面。如:http://localhost:8000/save_api/?project_id=1
this.参数名1:作为传参,已在bom层中声明并初始化的变量,如:setting_api:{},
this.参数名2:指已在bom层声明并初始化的变量,如:apis:[],已在dom层中引用,接口返回数据后,则会渲染后展示。
3、实现代码
save_api(){
axios.post('http://localhost:8000/save_api/',this.setting_api,{
params:{
project_id : this.project_id
}
}).then(res=>{
this.apis=res.data;
})
},
更多推荐
已为社区贡献6条内容
所有评论(0)