<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
fn = async () => {
await axios.get()
}
//axios底层原理:promise对象包裹 ajax原生代码
axios = {
get(url) {
return new Promise((resolve, reject) => {
const xhr= new XMLHttpRequest
xhr.open(url)
xhr.send()
xhr.onload=function(){
//JSON字符串转js对象
const res=JSON.parse(xhr.responseText)
resolve(res)
}
xhr.onerror=function(){
reject('请求错误')
}
})
},
}
// axios.get()的返回值是一个promise对象 因此axios.get()有then方法 也就是axios.get().then
</script>
</body>
</html>
axios.js 原理(axios.get()返回值是promise对象)
最新推荐文章于 2025-02-13 23:54:14 发布