Axios

安装

使用 npm:

1
$ npm install axios

使用 CDN:

1
2
3
4
5
<!-- jsDelivr CDN -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<!-- unpkg CDN -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

其他: https://www.axios-http.cn/docs/intro

使用

1
2
3
4
5
6
7
8
9
// 发起一个post请求
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});

.then() 内的函数是 成功回调函数

1
2
3
4
5
6
7
8
9
// 在 node.js 用GET请求获取远程图片
axios({
method: 'get',
url: 'http://bit.ly/2mTM3nY',
responseType: 'stream'
})
.then(function (response) { // 请求成功后执行的函数(成功回调函数)
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});

请求方式别名

axios(url[, config]) 默认 GET 请求

axios.get(url[, config]) GET

axios.post(url[, data[, config]]) POST

其他: https://www.axios-http.cn/docs/api_intro