zoukankan      html  css  js  c++  java
  • promise对象

    直接放大佬写的:https://es6.ruanyifeng.com/#docs/promise

    这边写一下怎么用promise去封装请求

    var urla =  'http://api.xuandan.com/DataApi/index?AppKey=3aw4643qpb&page=1&cid=';
        var de = '请求失败';
        var ajax = function(url){
          return   new Promise(function(resolve,reject){
                    var xhr = new XMLHttpRequest();

                    xhr.open('get',url);
                    xhr.send();

                    xhr.onreadystatechange = function(){
                        if(xhr.readyState == 4 && xhr.status == 200){
                            var res = JSON.parse(xhr.responseText);
                            resolve(res)

                        }else if(xhr.readyState !== 4 && xhr.status !== 200){
                            reject(de)
                        }
                    }


            })
        }

        ajax(urla).then(res => {
            console.log(res);
        }).catch(error =>{
            console.log(error);
        })
     
     
     
     
     
  • 相关阅读:
    MSCRM 2011 修改显示记录数
    医疗相关名词解析
    把图片中的文字转成文本
    自我介绍吧
    第三次作业
    第一次作业心得
    耿丹161第一次作业
    第二次作业
    C#常用函数表及Asp.net(C#)常用函数表
    C语言I博客作业02
  • 原文地址:https://www.cnblogs.com/chenyudi/p/12392273.html
Copyright © 2011-2022 走看看