Axios 请求发送和拦截
Axios是一个流行的基于 Promise 的HTTP客户端库,用于发起HTTP请求。它提供了一种简洁的方式来发送和处理HTTP请求,同时还提供了拦截器interceptors的功能,可以对请求和响应进行拦截处理。本文将整理Axios请求发送和拦截方面的知识,分别进行分类,并给出示例代码,并在代码中进行注解。
发送GET请求
// 发送GET请求
axios.get('/api/data')
.then(response => {
// 请求成功的处理逻辑
console.log(response.data);
})
.catch(error => {
// 请求失败的处理逻辑
console.error(error);
});
发送POST请求
// 发送POST请求
axios.post('/api/data', { name: 'John', age: 30 })
.then(response => {
// 请求成功的处理逻辑
console.log(response.data);
})
.catch(error => {
// 请求失败的处理逻辑
console.error(error);
});