zoukankan      html  css  js  c++  java
  • 原生Ajax + Promise

    有原生写的ajax + promise嫁接下

    ;(function(root){
        var LD = function(obj){
            if( obj instanceof LD ) return obj;
            if( !(this instanceof LD )) return new LD(obj);
        };
    
        root.LD = LD;
    
        console.log(window.LD);
    
        function __encodeParameter( data ){
            let tmp = "";
            if(typeof data === 'string')
                tmp = data;
            else{
                var e = encodeURIComponent;
                var params = [];
                for( let k in data ){
                    if(data.hasOwnProperty( k )){
                        params.push( e(k)) +'=' + e(data[k]);
                    }
                }
    
                tmp = params.join('&') ; //
            }
    
            return tmp;
        }
    
        function __initialize_xhr(){
            let xhr ;
            if(window.XMLHttpRequest){
                xhr = new XMLHttpRequest();
            }else if(window.ActiveXObject){
                try{
                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                }catch(ex){
                    console.log(ex);
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
            }
    
            return xhr;
        }
    
        var ajax = LD.Ajax = function( method , url , data , headers ,cb){
            let promise = new Promise(function(resolve , reject ){
                 let xhr , params;
                 data = data || {};
                 headers = headers || {};
    
                 try{
                     xhr  =  __initialize_xhr();
                 }catch(ex){
                     console.log(ex);
                 }
    
                 params = __encodeParameter( data );
                 console.log("-------"+   params);
    
                 if(method == 'GET' && params ){
                         url += '?' + params;
                         params = null;
                 }
    
                 xhr.open( method , url );
                 var content_type = 'application/x-www-form-urlencoded';
                 for(var h in headers ){
                     if(headers.hasOwnProperty(h)){
                         if(h.toLowerCase() === "content-type"){
                             content_type = headers[h];
                         }else{
                             xhr.setRequestHeader(h , headers[h]);
                         }
                     }
                 }
    
                 xhr.setRequestHeader('Content-type', content_type );
                 xhr.onreadystatechange  = function(){
                     if(xhr.readyState === 4 ){
                         var err = (!xhr.status || (xhr.status < 200 || xhr.status >= 300) &&
                                    xhr.status !== 304 );
                         if( err === false ){
                             resolve( xhr.responseText , xhr );
                         }else{
                             reject( err , xhr );
                         }
                         //cb(err, xhr.responseText,xhr );
                     }
                 };
    
                 xhr.send(params);
            });
    
            return promise;
        };
    })(window);

    使用

    LD.Ajax("GET","/refresh",null,null).then(function(data){
         console.log(data);
       },function( dt) {
        console.log("err:" + dt );
       }).catch(function(){
        console.log('catch error');
      });

  • 相关阅读:
    热修复之类加载机制总结
    socket之tcp如何维护长连接
    sqlite之多线程处理
    android主线程ActivityThread-转载
    线程之ThreadLocal使用
    图片之压缩总结
    线程之交替执行的实例
    git的最常用命令总结
    sqlite之常见的语句
    activity之分析-3分钟看懂Activity启动流程
  • 原文地址:https://www.cnblogs.com/visonme/p/5466170.html
Copyright © 2011-2022 走看看