Axios 是一個(gè)基于 promise 的 HTTP 庫(kù),可以用在瀏覽器和 node.js 中。
使用 npm:
$ npm install axios
使用 bower:
$ bower install axios
使用 CDN:
<script src="https://unpkg.com/axios/dist/axios.min.js" rel="external nofollow" ></script>
GET
請(qǐng)求
// 為給定 ID 的 user 創(chuàng)建請(qǐng)求
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// 上面的請(qǐng)求也可以這樣做
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
POST
請(qǐng)求
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(), getUserPermission()])
.then(axios.spread(function (acct, perms) {
// 兩個(gè)請(qǐng)求都執(zhí)行完成
}));
更多建議: