axios.get() 函数返回一个 promise 对象,因此可以使用 then() 方法来链式调用处理响应数据。例如,在获取 GitHub 用户数据时:
const axios = require('axios');
axios.get('https://api.github.com/users/octocat')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
当请求成功时,then() 回调函数将会输出响应数据。当请求失败时,catch() 回调函数将会输出错误信息。