当前位置: 首页 > wzjs >正文

什么是定制网站seo怎么推广

什么是定制网站,seo怎么推广,上海外贸企业,浦东建设网站制作一、解决回调嵌套/回调地狱(代码1-展示axios链式调用) 1.1 传统回调-回调地狱 如果没有Promise,我们可能需要这样写:axios({url: http://hmajax.itheima.net/api/province}).then((provinceResult) > {const pname provinceRe…

一、解决回调嵌套/回调地狱(代码1-展示axios链式调用)

1.1 传统回调-回调地狱

如果没有Promise,我们可能需要这样写:

        axios({url: 'http://hmajax.itheima.net/api/province'}).then((provinceResult) => {const pname = provinceResult.data.list[0];console.log('省份:', pname);axios({url: 'http://hmajax.itheima.net/api/city',params: { pname }}).then((cityResult) => {const cname = cityResult.data.list[0];console.log('城市:', cname);axios({url: 'http://hmajax.itheima.net/api/area',params: { pname, cname }}).then((areaResult) => {console.log('地区数据:', areaResult.data);}).catch((err) => {console.error('获取地区数据失败:', err);});}).catch((err) => {console.error('获取城市数据失败:', err);});}).catch((err) => {console.error('获取省份数据失败:', err);});

这种"回调地狱"让代码难以阅读和维护。

而promise给了我们一种拥有较强可读性与灵活性的解决方法:
该示例为axios集成函数,其底层为promise与xhr结合使用,返回值也为promise。

let pname = ''axios({url: 'http://hmajax.itheima.net/api/province'}).then((result) => {console.log(result);pname = result.data.list[0]console.log(result.data.list[0]);return axios({url: 'http://hmajax.itheima.net/api/city',params: {pname: `${result.data.list[0]}`}})}).then((result => {console.log(result);return axios({url: 'http://hmajax.itheima.net/api/area',params: {pname,cname: `${result.data.list[0]}`}})})).then(result => {console.log(result);})

1.2 链式调用实践

代码展示了典型的Promise链:

axios.get('/api/province').then(result => {           // 第一个thenpname = result.data.list[0];return axios.get('/api/city', {params: {pname}}); // 返回新Promise}).then(result => {           // 第二个thenreturn axios.get('/api/area', {params: {pname, cname: result.data.list[0]}});}).then(result => {           // 第三个thenconsole.log(result);});

每个then接收前一个Promise的解析值,并可以返回新Promise继续传递。
Promise通过链式调用解决了回调嵌套问题,让异步代码拥有了近乎同步代码的可读性。

二、Promise返回值详解(代码2-promise与xhr包装函数)

为了更好理解promise,我们用promise与xhr包装一个简单的axios函数:
同时为了帮助大家加强理解,尽量不使用语义化变量名

        function myaxios(lala) {//promise函数return new Promise((resolve, reject) => {//查询模式(有params)if (lala.params) {//传递查询参数const paramsobj = new URLSearchParams(lala.params)const stree = paramsobj.toString()lala.url += `?${stree}`}//配置xhrconst xhr = new XMLHttpRequest()xhr.open(lala.method || 'GET', lala.url)xhr.addEventListener('loadend', () => {if (xhr.status >= 200 && xhr.status < 300) {console.log(xhr.response);console.log(JSON.parse(xhr.response));resolve(JSON.parse(xhr.response))}else {reject(new Error(xhr.response))}})//请求格式(有data)if(lala.data){xhr.setRequestHeader('Contentt-Type','application/json')const streee=JSON.stringify(lala.data)xhr.send(streee)}else{xhr.send()}})}myaxios({url: 'http://hmajax.itheima.net/api/register',method:'POST',data:{username:'evergreen',password:'060722'}}).then((result) => {console.log(result);console.log('注册成功');}).catch((error) => {console.log(error);})

2.1 核心特性

在代码2实现中,myaxios函数返回一个Promise对象:

function myaxios(lala) {return new Promise((resolve, reject) => {// 异步操作if(成功) resolve(result);else reject(error);});
}

关键点

  1. 每个.then()都会返回新的Promise,允许继续链式调用
  2. 回调函数的返回值决定新Promise的状态:
    • 返回普通值 → Promise resolved
    • 返回Promise → 跟随该Promise状态
    • 抛出错误 → Promise rejected

三、Axios/myaxios返回值解析

3.1 响应结构

在代码1/2中,axios/myaxios返回的Promise解析值均为一个标准响应对象:

{data: {},       // 服务器返回的数据(自动JSON解析)status: 200,    // HTTP状态码statusText: 'OK', headers: {},    // 响应头config: {},     // 请求配置request: {}     // 底层请求对象
}

3.2 错误处理

axios自动将非2xx状态码视为reject,同时我们也在myaixios中实现:

 		if (xhr.status >= 200 && xhr.status < 300) {console.log(xhr.response);console.log(JSON.parse(xhr.response));resolve(JSON.parse(xhr.response))}else {reject(new Error(xhr.response))}

四、封装实践(代码2解析)

在实现自己的myaxios时,注意以下几点:

  1. Promise封装错误状态设置

    return new Promise((resolve, reject) => {// XHR操作if (xhr.status >= 200 && xhr.status < 300) {resolve(JSON.parse(xhr.response));} else {reject(new Error(xhr.response));}
    });
    
  2. 参数处理

    // 处理查询参数if (lala.params) {//传递查询参数const paramsobj = new URLSearchParams(lala.params)const stree = paramsobj.toString()lala.url += `?${stree}`}
    // 处理请求体if(lala.data){xhr.setRequestHeader('Contentt-Type','application/json')const streee=JSON.stringify(lala.data)xhr.send(streee)}else{xhr.send()
    
  3. 错误对象:使用new Error()包装错误信息,在程序而非语义上标记错误信息,便于调试

完整代码:

代码1:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>let pname = ''axios({url: 'http://hmajax.itheima.net/api/province'}).then((result) => {console.log(result);pname = result.data.list[0]console.log(result.data.list[0]);return axios({url: 'http://hmajax.itheima.net/api/city',params: {pname: `${result.data.list[0]}`}})}).then((result => {console.log(result);return axios({url: 'http://hmajax.itheima.net/api/area',params: {pname,cname: `${result.data.list[0]}`}})})).then(result => {console.log(result);})</script>
</body></html>

代码2:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>/* lala={url:''method:''} *///要封装的axiosfunction myaxios(lala) {//promise函数return new Promise((resolve, reject) => {//如果有if (lala.params) {//传递查询参数const paramsobj = new URLSearchParams(lala.params)const stree = paramsobj.toString()lala.url += `?${stree}`}//配置xhrconst xhr = new XMLHttpRequest()xhr.open(lala.method || 'GET', lala.url)xhr.addEventListener('loadend', () => {if (xhr.status >= 200 && xhr.status < 300) {console.log(xhr.response);console.log(JSON.parse(xhr.response));resolve(JSON.parse(xhr.response))}else {reject(new Error(xhr.response))}})//发送xhrif(lala.data){xhr.setRequestHeader('Contentt-Type','application/json')const streee=JSON.stringify(lala.data)xhr.send(streee)}else{xhr.send()}})}myaxios({url: 'http://hmajax.itheima.net/api/register',method:'POST',data:{username:'evergreen',password:'060722'}}).then((result) => {console.log(result);console.log('注册成功');}).catch((error) => {console.log(error);})</script>
</body></html>
http://www.dtcms.com/wzjs/26586.html

相关文章:

  • 高端建站咨询外链管理
  • 山东兴华建设集团有限公司网站拼音企业推广网络营销外包服务
  • wordpress ajax -1安阳企业网站优化外包
  • 广东网站建设公司报价表广州seo顾问服务
  • 网站联盟系统seo知识培训
  • 商城网站开发模板网络广告四个特征
  • 网页首页设计代码安徽网站关键字优化
  • 关于网站推广免费舆情监测平台
  • 建网站哪家质量好网站开发的一般流程
  • 网站开发建设公司seo搜索引擎优化总结
  • 用phpmysql做图书网站软文网站平台
  • 北京模板网站开发公司郑州百度快照优化
  • 网站建设方案下载google chrome谷歌浏览器
  • 网站如何改首页模块org域名注册
  • 东莞建工集团企业网站竞价推广渠道
  • 南京 公司网站制作整站优化seo平台
  • 自助建设手机网站百度识图鉴你所见
  • 如何在台湾做企业网站百度seo服务方案
  • 做外贸网站可以收付款吗北京百度推广代理公司
  • 襄樊做网站惠州seo博客
  • 北滘网站建设seo导航站
  • 苏州企业网站建设设计写文章一篇30元兼职
  • 建设银行网站为什么登不上seo快速排名点击
  • wordpress 博客搬家seo推广优化方案
  • 网站建站图片百度推广后台管理
  • 网站的日历怎么做怎么成为百度推广代理商
  • 做企业网站需要什么文件网站排名监控工具
  • 淘宝站外网站可以做吗培训心得体会范文大全1000
  • 百度小程序还需要做网站吗公司推广咨询
  • 安装wordpress要数据库优化大师破解版app