多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 一、概述 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。 ## 二、安装 ``` npm install axios ``` 或者直接引入js: ``` <script src="https://unpkg.com/axios/dist/axios.min.js"></script> ``` ## 三、GET请求 // 为给定 ID 的 user 创建请求 ``` axios.get('/user?ID=12345') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); // 上面的请求也可以这样做 axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); ``` ## 四、POST请求 ~~~ axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); ~~~ ## 五、多并发请求 ``` function getUserAccount() { return axios.get('/user/12345'); } function getUserPermissions() { return axios.get('/user/12345/permissions'); } axios.all([getUserAccount(), getUserPermissions()]) .then(axios.spread(function (acct, perms) { // 两个请求现在都执行完成 })); ```