作用:一个专门用来发送ajax请求的库,可以在浏览器或者node.js中使用
Promise based HTTP client for the browser and node.js
以Promise为基础的HTTP客户端,适用于:浏览器和node.js
封装ajax,用来发送请求,异步获取数据
使用步骤
1.本地安装axios: `npm i -g axios`
2.导入axios
3.使用
GET 方式发送请求
// 方式一(没参数):
axios.get('http://localhost:8888/test/index').then(res => {
console.log('获取数据:',res.data)
})
// 方式二(有参数):
axios.get('http://localhost:8888/menus',{
params:{
id:003
}
}).then(res => {
console.log('获取数据:',res.data)
})
POST 方式发送请求
// 方式一:
// 第一个参数表示接口地址,第二个参数表示接口需要的参数
axios.post('http://localhost:8888/test/index',{
name:'axios添加数据',
done:true
}).then(res => {
console.log('获取数据:',res.data)
})
其他方式发送请求
// 删除:
// delete
axios.delete('http://localhost:8888/test/index',{}).then(res => {
console.log(res)
})
// 更新:
// put
axios.put('http://localhost:8888/test/index',{
name: '111',
age: 300
}).then(res => {
console.log(res)
})
// 更新:
// patch
axios.patch('http://localhost:8888/test/index',{
age:30
}).then(res => {
console.log(res)
})