<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button id="btnPost">发起POST请求</button>
<button id="btnGet">发起GET请求</button>
<script src="axios.js"></script>
<script>
document.querySelector('#btnPost').addEventListener('click', async function () {
// 如果调用某个方法的返回值是Promise实例,则前面可以添加await!
// await只能用在被async修饰的方法中
const result = await axios({
method: 'POST',
url: '/axios/post',
data: {
name: 'zs',
age: 20
}
})
console.log(result)
})
document.querySelector('#btnGet').addEventListener('click', async function () {
// 如果调用某个方法的返回值是Promise实例,则前面可以添加await!
// await只能用在被async修饰的方法中
const {data: res} = await axios({
method: 'GET',
url: '/axios/get',
data: {
name: 'zs',
age: 20
}
})
console.log(res)
})
</script>
</body>
</html>
标签:axios,await,解构,result,async,data,赋值
From: https://www.cnblogs.com/yang-young-young/p/17479019.html