简介
什么是Axios?
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
特点
- 基于promise的异步ajax请求库
- 浏览器端/node端都可以使用
- 支持请求/响应拦截器
- 支持请求取消
- 请求/响应数据转换
- 批量发送多个请求
安装
1. 下载依赖包
npm install axios --save-D
2. 在需要的组件中引入
import axios from "axios" //导入axios
案例
const axios = require('axios');
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
axios基本使用
axios常用语法
nam | value |
---|---|
axios(config) | 通用/最本质的发任意类型请求的方式 |
axios(url[,config]) | 可以只指定url发get请求 |
axios.request(config) | 等同于axios(config) |
axios.get(url[,config]) | 发get请求 |
axios.delete(url[,config]) | 发d |