npm:
npm install axios
yarn:
yarn add axios
使用axios发送异步请求
发送GET请求
axios
.get('/test?page=2')
.then(function (response) { // 响应完成的钩子函数
// 响应的body在response.data中,如果是以json格式传回,则可以直接使用,response中还有一些其他的响应内容
})
.catch(function (error)) { // 产生错误的钩子函数
};// get传递参数的另外一种方式axios
.get('/test', { params: { page: 2
}
})
.then(function (response) { // ...
})
.catch(function (error) { // ...
});
发送POST请求
axios
.post('/test', { page: 2
})
.then(function (response) { // ...
})
.catch(function (error) { // ...
});
作者:Kindem
链接:https://www.jianshu.com/p/3b03eef0af9e