2021/10/27 北京 axios发送get和post无参有参请求

axios发送get无参请求

axios.get('http://localhost:9999/student/getAllStudent')
.then(res=>{
console.log(res)  
}).catch(err=>{
console.log(err)
})

axios发送get有参请求

axios.get('http://localhost:9999/student/getAllStudent',{params:{id:1}})
.then(res=>{
console.log(res)  
}).catch(err=>{
console.log(err)
})

axios发送post无参请求

axios.post('http://localhost:9999/student/getAllStudent')
.then(res=>{               //把get无参请求的get改为post即可
console.log(res)  
}).catch(err=>{
console.log(err)
})

axios发送post有参请求

axios.post('http://localhost:9999/student/getAllStudent',"name=张三&age=10")
.then(res=>{               //发送post请求携带参数,直接使用"name=张三&age=10"
console.log(res)  
}).catch(err=>{
console.log(err)
})

axios发送post有参请求

axios.post('http://localhost:9999/student/findStudentByName',{name:'张三'})
.then(res=>{               //发送post请求携带参数,这种传参方式后台得使用requestBody处理
console.log(res)  
}).catch(err=>{
console.log(err)
})

 


什么情况下会出错呢?

用data传参,后台不用requestbody接收的话,name会为null

axios({
url:''http://localhost:9999/student/findStudentByName',
method:'post',
 data:{
  name:'张三' //后台不用requestbody接收的话,name会为null
 }
}).then(res=>{
console.log(res)
})

axios.all发送并发请求,获取所有学生和指定id学生

axios.all([
axios.get('http://localhost:9999/student/findStudentById'),
axios.get('http://localhost:9999/student/getAllStudent',{params:{id:1}})
]).then(res=>{          
console.log(res[0]); //第一种方式使用数组的方式返回,res[0],res[1]返回请求结果
console.log(res[1])
}).catch(err=>{
console.log(err)
})

用axios.spread处理并发请求

axios.all([
axios.get('http://localhost:9999/student/findStudentById'),
axios.get('http://localhost:9999/student/getAllStudent',{params:{id:1}})
]).then(
axios.spread((res1,res2)=>{
console.log(res1)   //res1,res2获取俩个请求的结果,只是看起来更高大上
console.log(res2)
})          
).catch(err=>{
console.log(err)
})

axios全局配置baseURL和超时

axios.defaults.baseURL='http://localhost:9999/student/student';
axios.defaults.timeout=5000;  //5秒超时设置
2021-10-27北京街拍

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值