<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
// 前端开发中异步代码最多的就是发请求
async function main () {
// axios({
// method: 'GET',
// url: 'http://localhost:3000/heros'
// }).then(res => {
// console.log(res)
// })
try {
const heroRes = await axios({
method: 'GET',
url: 'http://localhost:3000/heros'
//这个地址是我本地的。
})
console.log(heroRes)
const postRes = await axios({
method: 'GET',
url: 'http://localhost:3000/posts'
//这个地址是我本地的。
})
console.log(postRes)
} catch (err) {
console.log('请求出错了', err)
}
}
main()
</script>
</body>
</html>
在async函数中使用axios
最新推荐文章于 2023-05-11 18:29:56 发布