axios的基本使用

一、常见请求方法

1、发送GET请求 — 不携带任何参数

//获取所有人的用户信息
getUserInfo() {
    axios.get('http://localhost:8080/persons').then(
    response => {console.log('请求成功', response.data)}, 
    error => {console.log('请求失败', error)})
}

2、 发送GET请求 — 携带query参数

//获取某个人的信息
getUserInfo() {
    axios.get('http://localhost:8080/persons',{params:{id:personId.value}}).then(
    response => {console.log('请求成功', response.data)}, 
    error => {console.log('请求失败', error)})
}

3、发送POST请求 — 携带body参数

//添加用户信息
addPeople() {
    axios.get('http://localhost:8080/addPersons',{name:name.value,age:age.value}).then(
    response => {console.log('请求成功', response.data)}, 
    error => {console.log('请求失败', error)})
}

4、发送PUT请求 – 携带body参数

//更新某一个人的信息
upDatePeople() {
    axios.get('http://localhost:8080/updatePersons',{id:personId.value.value,name:name.value,age:age.value}).then(
    response => {console.log('请求成功', response.data)}, 
    error => {console.log('请求失败', error)})
}

5、发送DELETE请求 – 携带params参数

//删除某个人的信息
deletePeople() {
    axios.delete('http://localhost:8080/deletePersons',{params:{id:personId.value}}).then(
    response => {console.log('请求成功', response.data)}, 
    error => {console.log('请求失败', error)})
}

二、axios拦截器

1、创建实例

// 创建实例
const axios = Axios.create({
    baseURL: 'xxxxxx' ,
    timeout: 60000
});

2、请求拦截


axios.interceptors.request.use(function (response) {
    console.log('响应拦截成功');
    return response;
}, function (error) {
    console.log('响应拦截失败');
    return Promise.reject(error);
});

3、响应拦截器

axios.interceptors.response.use(function (response) {
    console.log('响应拦截成功');
    return response;
}, function (error) {
    console.log('响应拦截失败');
    return Promise.reject(error);
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值