gp更新来的太快

意外总是会发生

添加一个判断function的分支,过滤掉function,其实也考虑再进一步去分析它的作用,稍后再议。

更新一下

var gnp = {
    get: function(url) {
        return this._get(url, true)
    },
    synget: function(url) {
        return this._get(url, false)
    },
    _get: function(url, syn) {
        const promise = new Promise(function(resolve, reject) {
            const handler = function() {
                if(this.readyState !== 4) {
                    return;
                }
                if(this.status === 200) {
                    resolve(this.response);
                } else {
                    reject(new Error(this.statusText));
                }
            }
            var request = new XMLHttpRequest()
            request.open('GET', url, syn)
            request.onreadystatechange = handler
            request.send(null)
        })
        return promise
    },
    post: function(url, data) {
        return this._post(url, data, true)
    },
    synpost: function(url, data) {
        return this._post(url, data, false)
    },
    _post: function(url, data, syn) {
        const promise = new Promise(function(resolve, reject) {
            const handler = function() {
                if(this.readyState !== 4) {
                    return;
                }
                if(this.status === 200) {
                    resolve(this.response);
                } else {
                    reject(new Error(this.statusText));
                }
            }
            var request = new XMLHttpRequest()
            request.open('POST', url, syn)            
            request.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            request.onreadystatechange = handler
            
            
            var ret = gnp._trans(data)
            
            request.send(ret.substr(0,ret.length-1))
        })
        return promise
    },
    _trans : function(data,key = ''){
        var ret = ""                
        if(typeof data == 'object'){
            for(let it in data){
                ret += gnp._trans(data[it],key + (key == ''?it:"["+ it + "]"))                    
            }
        }else if(Array.isArray(data)){
            for(var i = 0;i < data.length;i++){
                ret += gnp._trans(data[i],key + "[" + i + "]") 
            }        
        }else if(typeof data == 'function'){
            
        }else{
            console.log(data)
            ret +=encodeURIComponent( key) + '=' + encodeURIComponent(data)  + "&" 
        }
        return ret            
    }    
}

 

你可能感兴趣的:(gp更新来的太快)