北京外贸网站优化,高端科技产品网站建设,保定手机网站制作,莱芜网站建设设计外包【摘要】ES6-promise封装AJAX请求考必过小编为大家整理了关于ES6-promise封装AJAX请求的信息#xff0c;希望可以帮助到大家#xff01;ES6-promise封装AJAX请求标签#xff1a;const状态码setreject对象响应状态ISErequest// 接口地址:https://api.apiopen.top/getJoke// 1…【摘要】ES6-promise封装AJAX请求考必过小编为大家整理了关于ES6-promise封装AJAX请求的信息希望可以帮助到大家ES6-promise封装AJAX请求标签const状态码setreject对象响应状态ISErequest// 接口地址:https://api.apiopen.top/getJoke// 1.创建对象const xhr new XMLHttpRequest();// 2.初始化//发 GET类型的请求 给这个接口发请求https://api.apiopen.top/getJoke// xhr.open(GET,https://api.apiopen.top/getJoke);表示成功// xhr.open(GET,https://api.apiopen.top/getJoke);// xhr.open(GET,https://api.apiopen.top/get); 表示失败xhr.open(GET,https://api.apiopen.top/get);// 3.发送xhr.send();// 4.绑定事件处理响应结果xhr.onreadystatechangefunction(){// 对状态做出一个判断if(xhr.readyState4){// 判断响应状态码 200-300 2系列的响应状态码都为成功if(xhr.status 200 xhr.status 300){// 表示成功console.log(xhr.response);}else{// 如果失败console.error(xhr.status);}}}// 接口地址:https://api.apiopen.top/getJokeconst p new Promise((resolve,reject){// 1.创建对象const xhr new XMLHttpRequest();// 2.初始化//发 GET类型的请求 给这个接口发请求https://api.apiopen.top/getJokexhr.open(GET,https://api.apiopen.top/getJoke);// 3.发送xhr.send();// 4.绑定事件处理响应结果xhr.onreadystatechangefunction(){// 对状态做出一个判断if(xhr.readyState4){// 判断响应状态码 200-300 2系列的响应状态码都为成功if(xhr.status 200 xhr.status 300){// 表示成功 resolve修改promise的状态resolve(xhr.response);}else{// 如果失败reject(xhr.status);}}}})// 指定回调 结构清晰p.then(function(value){// 如果成功打印valueconsole.log(value);},function(reason){console.log(reason);})ES6-promise封装AJAX请求标签const状态码setreject对象响应状态ISErequest以上就是ES6-promise封装AJAX请求的内容更多资讯请及时关注考必过网站最新消息小编会第一时间发布大家考试加油