码迷,mamicode.com
首页 > 其他好文 > 详细

gp更新来的太快

时间:2018-11-16 13:34:09      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:urlencode   lse   als   分析   span   false   function   response   hand   

意外总是会发生

添加一个判断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更新来的太快

标签:urlencode   lse   als   分析   span   false   function   response   hand   

原文地址:https://www.cnblogs.com/allofalan/p/9968529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!