axios的基本使用

这篇博客详细介绍了axios的使用,从基础的GET和POST请求,到并发请求、全局配置、实例创建以及在Vue.js中的应用。还讨论了解决POST请求参数传递的问题和如何设置拦截器。

title: axios基础
date: 2021-01-28 11:23:46
tags: 技术

一.axios 的基本使用

axios发送get请求
//默认请求方式发送无参请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios({
        url:'http://localhost:3008/posts',
    }).then(res=>{
        console.log(res);
    })
</script>
默认使用get方式请求
//指定请求方式为get发送无参请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios({
        url:'http://localhost:3008/posts',
        method:'get'
    }).then(res=>{
        console.log(res);
    })
</script>
//发送get方式有参请求-直接在url后面拼接?id=1
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios({
        url:'http://localhost:3008/comments?id=1',
    }).then(res=>{
        console.log(res);
    })
</script>
//发送get方式有参请求-使用params
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios({
        url:'http://localhost:3008/comments',
        params:{
            id:'1'
        }
    }).then(res=>{
        console.log(res);
    })
</script>
axios发送post请求
//指定请求方式为post发送无参请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios({
        url:'http://localhost:3008/comments',
        method:'post'
    }).then(res=>{
        console.log(res);
    })
</script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios({
        url:'http://localhost:3008/comments',
        method:'post',
        data:{
            id:'3',
            name:'科技新闻',
            post: "2",
        }
    }).then(res=>{
        console.log(res);
    })
</script>

二.axios请求方式

//使用axios.get发送无参请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.get('http://localhost:3008/comments').then(res=>{
        console.log(res);
    }).catch(err=>{
        console.log(err);
    })
</script>
//使用axios.get发送有参请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.get('http://localhost:3008/comments',{params:{id:2}}).then(res=>{
        console.log(res);
    }).catch(err=>{
        console.log(err);
    })
</script>
//使用axios.post发送无参请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.post('http://localhost:3008/comments').then(res=>{
        console.log(res);
    }).catch(err=>{
        console.log(err);
    })
</script>
//使用axios.post发送有参请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.post('http://localhost:3008/comments',{id:4,body:"时尚新闻"}).then(res=>{
        console.log(res);
    }).catch(err=>{
        console.log(err);
    })
</script>
or
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.post('http://localhost:3008/comments',"id=4&body=时尚新闻").then(res=>{
        console.log(res);
    }).catch(err=>{
        console.log(err);
    })
</script>

axios使用post携带参数默认使用application/json

解决方法一:params属性进行数据传递

解决方法二:“name=张三&id=1”

解决方法三:服务端给接受到的参数加上@requestbody


三.axios的并发请求

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.all([
        axios.get('http://localhost:3008/comments'),
        axios.get('http://localhost:3008/posts')
    ]).then(
        axios.spread((res1,res2)=>{
            console.log(res1);
            console.log(res2);
        })
    ).catch(err=>{
        console.log(err);
    })
</script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.all([
        axios.get('http://localhost:3008/comments'),
        axios.get('http://localhost:3008/posts')
    ]).then(res=>{
            console.log(res[0]);
            console.log("----------")
            console.log(res[1]);
        }
    ).catch(err=>{
        console.log(err);
    })
</script>

四.axios的全局配置

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.defaults.baseURL='http://localhost:3008';
    //axios.defaults.timeout=5000;
    axios.get('comments').then(res=>{
        console.log(res);
    });
    axios.get('posts').then(res=>{
        console.log(res);
    })
</script>

五.axios的实例

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    let newVar = axios.create({
        baseURL:'http://localhost:3008',
        //timeout:5000
    });//创建axios的实例
    newVar({
        url:'comments'
    }).then(res=>{
        console.log(res);
    })
</script>

六.axios的拦截器

axios 给我提供了两大拦截器 
一种是请求方向的拦截(成功请求,失败请求)
一种是响应方向的(成功响应,失败响应)
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.interceptors.request.use(config=>{
        console.log(config);//可以在这里加载动画
        return config;
    },err=>{
        console.log(err);
    })
    axios.get("http://localhost:3008/posts").then(res=>{
        console.log(res);
    })
</script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.interceptors.response.use(config=>{
        console.log(config);//可以在这里加载动画
        return config.data;//拦截后放行
    },err=>{
        console.log(err);

    })
    axios.get("http://localhost:3008/posts").then(res=>{
        console.log(res);
    })
</script>

七.axios在Vue中的模块封装

//封装位置 /src/network/request/request.js
import axios from "axios";
export function request(config,success,fail){
  axios({
    url:config
  }).then(res=>{
    success(res);
  }).catch(err=>{
    fail(err);
  })
}

//调用位置
import {request} from "./network/request/request";
request(
  "http://localhost:3008/comments",
  res=>{
    console.log(res);
  },
  res=>{
    console.log(err);
  }
)

//封装位置
import axios from "axios";
export function request(config){
axios.defaults.baseURL="http://localhost:3008"
  axios(config.url).then(
    res=>{
      config.success(res);
    }
  ).catch(err=>{
      config.fail(res);
  })
}
//调用位置
import {request} from "./network/request/request";
request({
  url:'comments',
  success:res=>{
    console.log(res);
  },
  fail:err=> {
    console.log(err);
  }}
)

最好的方法
//封装位置
import axios from "axios";
export function request(config){
  let newVar = axios.create({
    baseURL:"http://localhost:3008",
    timeout:5000
  });
  return newVar(config)
}

//调用位置
import {request} from "./network/request/request";
request({
  url:"comments"
}).then(res=>{
  console.log(res);
}).catch(err=>{
  console.log(err);
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值