對(duì)原生ajax的簡(jiǎn)單封裝

1.引用類型Http

'use strict';

//引用類型Http,封裝了ajax異步調(diào)用get和post方法
function Http() {
    this.xhq = null;
}

//啟動(dòng)http中g(shù)et請(qǐng)求,以備發(fā)送
Http.prototype.get = function (url, query, option) {
    this.request('get', url, query, null, option);
    return this;
}

//啟動(dòng)http中post請(qǐng)求,以備發(fā)送
Http.prototype.post=function(url,option){
    this.request('post',url,null,null,option);
    return this;
}

//發(fā)送http請(qǐng)求的通用方法
Http.prototype.request = function (method, url, query, option) {
    this.xhq=new XMLHttpRequest();
    var url = this.addQuery(url, query);
    //console.log(url);
    this.xhq.open(method, url, true);

    for (var i in option) {
        this.xhq.setRequestHeader(i, option[i]);
    }
    return this;
}

//獲取響應(yīng)成功后執(zhí)行
Http.prototype.success=function(success){
    this.then(success);
    return this;
}

//獲取響應(yīng)失敗后執(zhí)行
Http.prototype.fail=function(fail){
    this.then(fail);
    return this;
}

//獲取響應(yīng)后,根據(jù)響應(yīng)結(jié)果執(zhí)行相應(yīng)操作
Http.prototype.then = function (success,fail) {
    var xhq=this.xhq;
    xhq.addEventListener('readystatechange',function(){
         if (xhq.readyState === 4) {
            if (xhq.status >= 200 && xhq.status < 300 || xhq.status == 304) {
                if (typeof success === 'function') {
                    success(xhq.status, xhq.responseText);
                }
            } else {
                if (typeof fail === 'function') {
                    fail(xhq.status, xhq.responseText);
                }
            }
        }
    });
    return this;
}

//發(fā)送相應(yīng)請(qǐng)求
Http.prototype.send=function(data){
    this.xhq.send(JSON.stringify(data));
}

//為url添加查詢字符串輔助方法
Http.prototype.addQuery = function (url, data) {
    for (var i in data) {
        url += (url.indexOf('?') === -1 ? '?' : '&');
        url += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]);
    }
    return url;
}

2.測(cè)試代碼

var http=new Http();
http.request('get','text.json',null,{key: 'value'})
    .then(function(status,data){
        console.log(data);
    },function(status,data){
        console.log(status);
    })
    .success(function(status,data){
        console.log(status);
    })
    .send(null);

http.post('text.json',{name: 'jc',num: 1})
    .then(function(status,data){
        console.log(data);
    },function(status,data){
        console.log(status);
    })
    .success(function(){
        console.log('ready');
    })
    .send({name: 'jc',num: 1});
  • 紅寶書中提到,必須在調(diào)用open之前指定readystatechange事件處理程序才能確保瀏覽器兼容性,這里為了能夠鏈?zhǔn)秸{(diào)用在open之后通過DOM2級(jí)方法綁定了多個(gè)事件處理程序,在chorme下測(cè)試通過,應(yīng)該還有更好的實(shí)現(xiàn)方法
  • 在過程函數(shù)中返回this對(duì)象自身的引用達(dá)成鏈?zhǔn)秸{(diào)用,但是并沒有檢查輸入?yún)?shù)的類型,可能會(huì)導(dǎo)致問題
  • 可以使用anywhere搭建一個(gè)簡(jiǎn)單的本地服務(wù)器,測(cè)試ajax方法
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容