1.Axios是基于promise的一个HTTP库,可以用于浏览器与node.js中
Axios的特点:
从浏览器中创建 XMLHttpRequests
从 node.js 创建 http 请求
支持 Promise API
拦截请求和响应
转换请求数据和响应数据
取消请求
自动转换 JSON 数据
客户端支持防御 XSRF
2.安装axios
使用npm npm install axios
使用bower bower install axios
使用 cdn
3.执行GET请求
//给给定的ID的user创建请求
axios.get('/user?ID=12345')
.then(function(res){
console.log(res);
}).catch(function(err){
console.log(err);
})
执行 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) {
// 两个请求现在都执行完成
}));