第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

javascript原生ajax類(lèi)

標(biāo)簽:
JavaScript

  1. 代码:

    function ajax(){this.xhrthis.datathis.strtojson = function(str){    var arr = str.split('&')    var json = {}    for(var i in arr){        var k = arr[i].split('=')[0]        var v = arr[i].split('=')[1]        json[k] = v    }    return json}this.jsontostr = function(json){    var arr = new Array()    for(var k in json){        var v = json[k]        var val = k+'='+v        arr.push(val)    }    var str = arr.join('&')    return str}this.parseurl = function(url){    var reg = /(?:([A-Za-z]+):)?(\/{0,3})?(?:(.*):(.*)@)?([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?/    var res = reg.exec(url)    var scheme = (res[1])?res[1]:'http'    var user = (res[3])?res[3]:''    var pass = (res[4])?res[4]:''    var host = (res[5])?res[5]:''    var port = (res[6])?res[6]:''    var path = (res[7])?res[7]:''    var query = (res[8])?res[8]:''    var fragment = (res[9])?res[9]:''    var ret = {        scheme:scheme,        host:host,        port:port,        user:user,        pass:pass,        path:path,        query:query,        fragment:fragment    }    return ret}this.init = function(){    if(window.XMLHttpRequest){        this.xhr=new XMLHttpRequest()    }else{        this.xhr=new ActiveXObject("Microsoft.XMLHTTP")    }    return this}this.before = function(func){    this.xhr.addEventListener('readystatechange',function(){        if(this.readyState < 4){            func()        }    })    return this}this.success = function(func){    this.xhr.addEventListener('readystatechange',function(){        if (this.readyState == 4 && this.status == 200){            var res            if(this.getResponseHeader('content-type')==='application/json'){                res = JSON.parse(this.responseText)            }else{                res = this.responseText            }            func(res)        }    })    return this}this.error = function(func){    this.xhr.addEventListener('readystatechange',function(){        if(this.readyState == 4 && this.status != 200){            this.xhr.abort()            func()        }    })    return this}this.progress = function(func){    this.xhr.upload.addEventListener('progress',function(e){        if(e.lengthComputable){            var loading = Math.round(e.loaded / e.total * 100);            func(loading)        }    })    return this}this.request = function(obj){    var data = obj.data    var url = obj.url    var type = obj.type    var async = obj.async    var auth = obj.auth    this.init()    if(type == 'post'){        this.xhr.open(type,url,async)        this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")        if(data instanceof Object){            if(data instanceof FormData){                var obj = {}                data.forEach(function(val,key){                    if((key != 'undefined') && (val != 'undefined')){                        obj[key] = val                    }                })                data = this.jsontostr(obj)            }else{                data = this.jsontostr(data)            }        }    }else if(type == 'get'){        var info = this.parseurl(url)        var scheme = info.scheme        var host = info.host        var path = info.path        var query = info.query        if(data instanceof Object){            if(data instanceof FormData){                var obj = {}                data.forEach(function(val,key){                    if((key != 'undefined') && (val != 'undefined')){                        obj[key] = val                    }                })                data = this.jsontostr(obj)            }else{                data = this.jsontostr(data)            }        }        var psep = (path)?'/':''        var sep = (query || data)?'?':''        url = scheme+'://'+host+psep+path+sep+query+data        data = ''        this.xhr.open(type,url,async)    }else if(type == 'json'){        this.xhr.open('post',url,async)        this.xhr.setRequestHeader("Content-type", "application/json")        if((typeof(data)=='string')||(data instanceof String)){            data = this.strtojson(data)        }else if(data instanceof Object){            if(data instanceof FormData){                var obj = {}                data.forEach(function(val,key){                    if((key != 'undefined') && (val != 'undefined')){                        obj[key] = val                    }                })                data = obj            }        }        data = JSON.stringify(data)    }else if(type == 'file'){        this.xhr.open('post',url,async)        if((typeof(data)=='string')||(data instanceof String)){            return false        }else if(data instanceof Object){            if(!data instanceof FormData){                return false            }        }    }    if(auth){        this.xhr.withCredentials = true        this.xhr.setRequestHeader("Authorization", "Basic " + btoa(auth))    }    this.data = data    return this}this.send = function(){    this.xhr.send(this.data)}}
  2. 用法:

    var ajax = new ajax()var data = {data:'name=lee&sex=male',url:'http://localhost/test.php',type:'post',async:true}ajax.request(data).progress(function(loading){console.log(loading)}).success(function(data){console.log(data)}).error(function(){console.log(2)}).send()


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消