学习Axios笔记(1)

axios请求方法: get,post,put,patch,delete

get 获取数据
post 提交数据(表单提交+文件上传)
put 更新(所有数据推送到后端)
patch(只将修改的数据推送到后端)
delete: 删除数据

get方式请求

get方式的两种不同写法
way1:

 axios.get('/api'{
     params:{
         id: 12
     }
 }).then((res)=>{
     console.log(res);
 })
 
way2:
 axios({
    method:'get',
    url:'/api',
    params:{
        id: 12
    }
 }).then((res)=>{
     console.log(res);
 })
    

post方式

一般提交内容分为两种方式

  • form-data 表单提交(图片上传、文件上传)
  • application/json 格式
application/json格式的post请求发送:

way1:
let data ={
    id:12
}
axios.post('/post',data).then((res)=>{
     console.log(res);
 })
 
 way2:
axios({
    method:'post',
    url:'/api',
    data: data,
 }).then((res)=>{
     console.log(res);
 })
 
form-data方式发送post请求
let formData = new FormData();
for(let key in data){
    formData.append(key,data[key]);
}
axios.post('/post',formData).then((res)=>{
     console.log(res);
 })

put方式和patch方式

axios.put('/put',data).then((res)=>{
 console.log(res);
})
 
 
axios.patch('/patch',data).then((res)=>{
 console.log(res);
})

delete方式

这里区分参数是放在url中 还是放在请求体里

id是包含在url中
axios.delete('/delete',{
    params:{
        id:12
    }
}).then((res)=>{
    console.log(res);
})
 
 id是放在请求体中的
axios.delete('/delete',{
    data:{
        id:12
    }   
}).then((res)=>{
    console.log(res);
})


axios({
    method:'delete',
    url:'/api',
    data: data, //参数放请求体中
 }).then((res)=>{
     console.log(res);
 })
 
 
 axios({
    method:'delete',
    url:'/api',
    params: params, //参数放url中
 }).then((res)=>{
     console.log(res);
 })

axios处理并发请求

请求多个接口数据

axios.all([
        axios.get('/data.json'),
        axios.get('/city.json')
    ]).then(axios.spread(result1,result2)=>{
        //业务逻辑处理
    })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值