1.XMLHttprequst
//1.创建XMLHttpRequst对象 const XHR = new XMLHttpRequest //2.配置请求方法和请求url地址 XHR.open('GET','http://hmajax.itheima.net/api/province') //3.监听loadend事件,接受响应结果 XHR.addEventListener('loadend',()=>{ console.log(XHR.response) JSON.parse(XHR.response) }) //4.发送请求 XHR.send()
运行结果:
2.XMLHttprequst中携带参数运行结果://在XMLHttpRequst对象中携带参数 //1.创建XMLHttpRequst对象 const XHR = new XMLHttpRequest //2.配置请求方法和请求url地址 XHR.open('GET','http://hmajax.itheima.net/api/city?pname=辽宁省') //3.监听loadend事件,接受响应结果 XHR.addEventListener('loadend',()=>{ console.log(XHR.response) JSON.parse(XHR.response) }) //4.发送请求 XHR.send()
3.XMLHttprequst中携带多个参数
//在XMLHttpRequst对象中携带多个参数 const obj={ pname:'北京', cname:'北京市' } //将参数转位url码 const paramsObj = new URLSearchParams(obj) const newObj = paramsObj.toString() //1.创建XMLHttpRequst对象 const XHR = new XMLHttpRequest //2.配置请求方法和请求url地址 XHR.open('GET',`http://hmajax.itheima.net/api/area?${newObj}`) //3.监听loadend事件,接受响应结果 XHR.addEventListener('loadend',()=>{ console.log(XHR.response) JSON.parse(XHR.response) }) //4.发送请求 XHR.send()
运行结果:
4.XMLHttprequst提交数据
//在XMLHttpRequst传递参数 const user={ username:'itheima9167', password:'7654321' } //将参数转位json数据 const paramsObj = JSON.stringify(user) //1.创建XMLHttpRequst对象 const XHR = new XMLHttpRequest() //2.配置请求方法和请求url地址 XHR.open('POST',`http://hmajax.itheima.net/api/register`) //设置请求头 XHR.setRequestHeader('Content-Type','application/json') //3.监听loadend事件,接受响应结果 XHR.addEventListener('loadend',()=>{ console.log(XHR.response) JSON.parse(XHR.response) }) //4.发送请求 XHR.send(paramsObj)
运行结果:
使用promise 管理异步对象
什么是promise?
表示管理一个异步操作最终状态和结果值的对象
为什么要学习promise?
成功和失败状态,可以关联对应的处理程序,了解axios内部原理
Promise使用步骤
const p = new Promise((res,rej)=>{ setTimeout(()=>{ //模拟成功 res('执行成功') },500) }) p.then((res)=>{ console.log(res) }).catch(error=>{ console.log(error) })
执行结果:
const p = new Promise((res,rej)=>{ setTimeout(()=>{ //模拟成功 // res('执行成功') //模拟失败 rej('执行失败') },500) }) p.then((res)=>{ console.log(res) }).catch(error=>{ console.log(error) })
运行结果:
Promise的三种状态
使用Pormise管理XMLHttprequs
const p = new Promise((res,rej)=>{ const XHR = new XMLHttpRequest() XHR.open('GET','http://hmajax.itheima.net/api/province') XHR.addEventListener('loadend',()=>{ if (XHR.response.status == 200) { res(XHR.response) } else{ rej(new Error(XHR.response)) } }) XHR.send() }) p.then(res=>{ console.log(res) }).catch(error=>{ console.log(error) })
运行结果:
封装简易axios
const p = (config)=>{ return new Promise((res,rej)=>{ const XHR = new XMLHttpRequest() XHR.open(config.methods||'GET',config.url) XHR.addEventListener('loadend',()=>{ if (XHR.response.status == 200) { res(XHR.response) } else{ rej(new Error(XHR.response)) } }) XHR.send() }) } p({ url:'http://hmajax.itheima.net/api/province' }).then(res=>{ console.log(res) }).catch(error=>{ console.log(error) })
运行结果:
封装POST方式携带参数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> </body> <script> const p = (config)=>{ return new Promise((res,rej)=>{ const XHR = new XMLHttpRequest() XHR.open(config.methods||'GET',config.url) XHR.addEventListener('loadend',()=>{ if (XHR.response.status == 200) { res(XHR.response) } else{ rej(new Error(XHR.response)) } }) if(config.data){ const dataUrl = JSON.stringify(config.data) XHR.setRequestHeader('Content-Type','application/json') XHR.send(dataUrl) }else{ XHR.send() } }) } p({ url:'http://hmajax.itheima.net/api/register', methods:'POST', data:{ username:'ithei21312323123', password:'123123123' } }).then(res=>{ console.log(res) }).catch(error=>{ console.log(error) }) </script> </html>
运行结果:
使用promise链式调用解决回调地狱
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script>
const pointer = ''
const p = (config)=>{
return new Promise((res,rej)=>{
const XHR = new XMLHttpRequest()
XHR.open(config.methods||'GET',config.url)
XHR.addEventListener('loadend',()=>{
// if (XHR.response.status == 200) {
res(XHR.responseText)
// } else{
// rej(new Error(XHR.response))
// }
})
if(config.data){
const dataUrl = JSON.stringify(config.data)
XHR.setRequestHeader('Content-Type','application/json')
XHR.send(dataUrl)
}else{
XHR.send()
}
})
}
const that = this
p({
url:'http://hmajax.itheima.net/api/province'
}).then(res=>{
const resList = JSON.parse(res)
console.log(resList)
this.pointer = resList.list[0]
return p({url:'http://hmajax.itheima.net/api/city?pname='+this.pointer})
}).then(res => {
console.log(res)
})
</script>
</html>
运行结果:
使用async await解决回调地狱
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>使用async await解决回调地狱</title> </head> <body> </body> <script src="https://fastly.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script> const province = null const getProvince = async()=> { this.province = await axios({url:'http://hmajax.itheima.net/api/province'}) console.log(this.province.data.list[0]) } const getCity = async()=> { const city = await axios({url:'http://hmajax.itheima.net/api/city',params:{pname:this.province}}) console.log(this.city) } getProvince() getCity() </script> </html>
运行结果:
使用 try catch 捕获错误
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> </body> <script src="https://fastly.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script> const getProvince = async()=> { try{ const res = await axios({url:'http://hmajax.itheima.net/api/province1'}) console.log(res) }catch(e){ //TODO handle the exception console.log(e) } } getProvince() </script> </html>
运行结果:
事件循环 异步任务直接进入宿主环境进行倒计时,完成后进入任务队列,同步任务直接进入调用栈,执行任务。调用栈中没任务后循环,将任务队列中的任务推入调用栈
运行结果:1 3 5 2 4
2最后执行
promise.all 将所有promise对象合在一起,全部执行成功后进入.then,有任何一个失败都触发catch