ajax库Axios的使用方法
axios简介
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
axios的基本使用
如何引入axios
可以通过npm安装来进行使用
npm install axio
也可以使用 bower进行安装,然后在页面中进行引入:
bower install axios
还可以使用CDN来进行引入到页面中去
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
如何应用axios
当我们引入了axios之后,接下来我们就要开始正式的使用axios了,对于axios的使方法比较多:
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);
});
