ajax方法

[javascript] 
var Ajax = (function () { 
    //XMLHttpRequest對象 
    var _xMLHttpRequest = null, 
            _funData = {/*私有方法所需的全局變量*/ 
                readyState: 1 
            }, 
            _fun = {/*私有方法*/ 
                sendArguments: function (d) { 
                    if (typeof d === "object" && d != null) { 
                        var _d = []; 
                        for (var i in d) { 
                            _d.push(encodeURIComponent(i) + "=" + encodeURIComponent(d[i])); 
                        } 
                        return _d.join('&'); 
                    } 
                    return null; 
                }, 
                createXMLHttpRequest: function () {/*創建XMLHttpRequest*/ 
                    if (!_xMLHttpRequest) { 
                        if (XMLHttpRequest) { 
                            _xMLHttpRequest = new XMLHttpRequest(); 
                        } 
                        else { 
                            if (ActiveXObject) { 
                                try { 
                                    _xMLHttpRequest = new ActiveXObject("Msxm12.XMLHTTP"); 
                                } 
                                catch (e) { 
                                    _xMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
                                } 
                            } 
                        } 
                        return _xMLHttpRequest; 
                    } 
                    else { 
                        _fun.createXMLHttpRequest = function () { 
                            return _xMLHttpRequest; 
                        } 
                    } 
 
                }, 
                extend: function () {/*遞歸合並對象*/ 
                    var i = arguments.length; 
                    if (i > 0 || typeof arguments[0] !== "object") { 
                        var _o = {}, 
                        j = 1; 
                        for (; j < i; j++) { 
                            if (typeof arguments[j] === "object") { 
                                for (var _i in arguments[j]) { 
                                    arguments[0][_i] = arguments[j][_i]; 
                                } 
                            } 
                        } 
                    } 
                    return arguments[0]; 
                }, 
                onreadystatechangeData: [/*監控執行過程函數集合*/ 
                     function () {//readyState=0 
                         _funData.readyState = 1; 
                         $("body").append("正在創建Ajax對象<br/>"); 
                     }, 
                     function () {//readyState=1 
                         _funData.readyState = 2; 
                         $("body").append("readyState=“" + _xMLHttpRequest.readyState + "”Ajax對象創建完成!等待open……<br/>"); 
                     }, 
                     function () {//readyState=2 
                         _funData.readyState = 3; 
                         $("body").append("readyState=“" + _xMLHttpRequest.readyState + "”open已發送,等待請求響應<br/>"); 
                     }, 
                     function () {//readyState=3 
                         _funData.readyState = 4; 
                         $("body").append("readyState=“" + _xMLHttpRequest.readyState + "”正在接受響應數據,等待完成<br/>"); 
                     }, 
                     function () {//readyState=4 
                         _funData.readyState = 0; 
                         var responseText = _xMLHttpRequest.responseText; 
                         if (_xMLHttpRequest.status === 200) { 
                             $("body").append("readyState=“" + _xMLHttpRequest.readyState + "”響應完成,執行CallBack<br/>"); 
                             if (_data.success != null) { 
                                 _data.success(responseText); 
                             } 
                         } else { 
                             $("body").append("readyState=“" + _xMLHttpRequest.readyState + "”status=“" + _xMLHttpRequest.status + "”響應完成,但是發生瞭異常!<br/>"); 
                             if (_data.error != null) { 
                                 _data.error(responseText); 
                             } 
                         } 
                     } 
                 ], 
                onreadystatechange: function () {/*readyState狀態改變觸發事件*/ 
                    /*
                    存在執行過程中每個狀態的處理函數
                    並且確保每個狀態隻執行一次處理函數(用於兼容狀態3在不同瀏覽器下 響應次數不同,比如IE下隻會響應1次狀態3,而FF會根據數據量執行多次)
                    */ 
                    //alert(_xMLHttpRequest.readyState + "+" + _funData.readyState) 
                    if (_fun.onreadystatechangeData[_xMLHttpRequest.readyState] && _xMLHttpRequest.readyState === _funData.readyState) { 
                        _fun.onreadystatechangeData[_xMLHttpRequest.readyState].call(this); 
                    } 
                }, 
                ajax: function () {/*提供給接口的方法*/ 
                    var fun = _fun, data = _data; 
                    fun.extend(data, arguments[0]); 
                    var _t = data.type.toLocaleLowerCase(); 
                    if (!(_t === "get" || _t === "post" || _t === "head")) { 
                        data.type = "get"; 
                    } 
                    this.XMLHttpRequest = _fun.createXMLHttpRequest(); 
                    this.XMLHttpRequest.onreadystatechange = fun.onreadystatechange; 
                    var url = data.url; 
                    if (data.type === "get") { 
                        url = url + (url.indexOf('?') > 0 ? "&" : "?") + fun.sendArguments(data.data); 
                        data.data = null; 
                    } 
                    this.XMLHttpRequest.open(data.type, url, true); 
                    this.XMLHttpRequest.setRequestHeader("Content-Type", "text/x-www-form-urlencoded"); 
                    this.XMLHttpRequest.send(fun.sendArguments(data.data)); 
 
                } 
            }, 
             _data = {/*參數*/ 
                 url: null, 
                 type: "get", 
                 data: null, 
                 dataType: "json", 
                 success: null, 
                 error: null 
             };  www.aiwalls.com
    return _fun.ajax; 
})(); 

使用方式
[javascript] 
Ajax({ 
            url: "/Test.aspx?type=a&a=b", 
            type: "get", 
            data: { a: 1, b: 2 }, 
            success: function (msg) { 
                alert(msg) 
            } 
        }) 

作者:chaiyining007

You May Also Like