# axios

axios 是一个专注于网络请求的库

# axios 的基本语法如下 :

1
2
3
4
5
6
7
axios({
method: '请求的类型',
url: '请求的URL地址'
}).them((result) => {
// .then 用来指定请求成功之后的回调函数
// 形参中的result是请求成功之后的结果
})
1
2
3
4
5
6
7
8
9
10
11
12
13
<script>
const btn = document.querySelector('button')
btn.addEventListener('click', async function () {
const {data} = await axios({
method: 'get',
url: 'http://localhost:8080/employees',
param: '123'
}).then(({data: res})=>{
console.log(res.data)
})
console.log(data.data)
})
</script>

async 表示将当前函数包装成一个 promise 对象,而里面可以使用 await, 表示一直等待直到拿到结果

如果不使用 await, 则拿到的 data 为 : Promise { <state>: "pending" }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script>
const btn = document.querySelector('button')
btn.addEventListener('click', function () {
const data = axios({
method: 'get',
url: 'http://localhost:8080/employees',
param: '123'
})
console.log(data)
// console.log(data.data)
})

axios.post()
</script>
更新于 阅读次数