Ajax中對XMLHttpRequset對象的封裝

來源:互聯網
上載者:User

標籤:ajax   xmlhttprequest   javascript   

    Ajax是非同步Javascript和XML,可以使網頁實現非同步更新。其中XMLHttpRequest對象是Ajax的web應用程式中一個關鍵的功能。

    簡單的使用是先建立XMLHttpRequset對象,註冊回呼函數,然後用open方法設定伺服器端互動的基本資料,設定發送的資料開始和伺服器端互動最後在回呼函數中判斷互動是否結束,響應是否正確,並根據需要擷取伺服器端返回的資料,更新頁面內容。

     

    但是對它的擴充中會存在很多問題,比如瀏覽器緩衝問題,中文亂碼問題和跨域訪問的問題。

    簡單的事情重複做,重複的事創造性的做。所以我們想到了把它封裝起來,用的時候直接引入。

    

/*封裝的xmlhttprequeset*/var MyXMLHttpRequest = function(){    var xmlhttprequest;    if(window.XMLHttpRequest){        xmlhttprequest = new XMLHttpRequest();        if(xmlhttprequest.overrideMineType){            xmlhttprequest.overrideMineType("text/xml");        }    }else if (window.ActiveXObject) {        var activeName = ["XSXML2.XMLHTTP","Microsoft.XMLHTTP"];        for (i = 0; i < activeName.length; i++) {            try {                xmlhttprequest = new ActiveXObject(activeName[i]);                break;            } catch (e) {                             }        }    }    if (xmlhttprequest === undefined || xmlhttprequest === null) {        alert("xmlhttprequest對象建立失敗!");    }else{        this.xmlhttp = xmlhttprequest;    }};// 使用者發送請求的方法MyXMLHttpRequest.prototype.send = function(method,url,data,callback,failback){    if (this.xmlhttp !== undefined || this.xmlhttp !== null) {        method = method.toUpperCase();        if (method !== "GET" && method !== "POST") {            alert("HTTP要求方法必須是GET或POST");            return ;        }        if (url === undefined || url === null) {            alert("http請求地址必須設定");            return;        }            var tempxmlhttp = this.xmlhttp;        tempxmlhttp.onreadystatechange = function(){            if (tempxmlhttp.readyState === 4) {                if (tempxmlhttp.status === 200) {                    var responseText = tempxmlhttp.responseText;                    var responseXML = tempxmlhttp.responseXML;                    if (callback === undefined || callback === null) {                        alert("沒有設定處理資料正確返回的方法");                        alert("返回的資料:" + responseText);                    }                    else{                        callback(responseText,responseXML);                    }                }else {                    if (failback === undefined || failback === null) {                        alert("沒有設定處理資料返回失敗的處理方法");                        alert("Http響應碼資訊:" + tempxmlhttp.status + ",響應碼文本資訊:" + tempxmlhttp.statusText);                        return ;                    }else{                        failback(tempxmlhttp.status,tempxmlhttp.statusText);                    }                }            }        };        // 解決緩衝的轉換        if (url.indexOf("?") >= 0) {            url = url + "&t=" + (new Date()).valueOf();        }else{            url = url + "?t=" + (new Date()).valueOf();        }        // 解決跨域問題        if (url.indexOf("http://") >= 0) {            url.replace("?","&");            url = url + "Proxy?url=";        }        this.xmlhttp.open(method,url,true);            // 如果是POST方式,需要佈建要求頭        if (method === "POST") {            this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        }        this.xmlhttp.send(data);    }else{        alert("XMLHttpRequest對象建立失敗,無法發送資料.");    }};MyXMLHttpRequest.prototype.abort = function(){    this.xmlhttp.abort();};
             這樣封裝起來之後用的時候只要調用方法,傳參數就可以了。 

    近段時間的感悟就是深深的體會了不要老想跟著學什麼特效,絢麗的特效終究還是js。無論是Jquery還是ajax主要還是寫javascript的代碼,好的介面是有好的想法然後運用這些語言去實現。

Ajax中對XMLHttpRequset對象的封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.