data retieval

View Code
//MIXINS
    //module pattern
    //I define a function and then immediately execute it within it's own private bubble (function context)
    (function () {
        //Data Retrieval
        var XMLHttpFactories = [
            function () { return new XMLHttpRequest() },
            function () { return new ActiveXObject('Msxml2.XMLHTTP') },
            function () { return new ActiveXObject('Msxml3.XMLHTTP') },
            function () { return new ActiveXObject('Microsoft.XMLHTTP') }
        ];
        
        function createXMLHTTPObject() {
            var xmlhttp = false;
            for (var i = 0; i < XMLHttpFactories.length; i++) {
                try {
                    xmlhttp = XMLHttpFactories[i]();
                } catch (ex) {
                    continue;
                }
                break;
            }
            return xmlhttp;
        }
        
        /*
            {String} url
            {Boolean} postData
            {Callback} callback
        
*/
        function sendRequest(url, postData, callback) {
            var req = createXMLHTTPObject();
            if (!req) return;
            var method = (postData) ? 'POST' : 'GET';
            req.open(method, url, true);
            req.setRequestHeader('User-Agent', 'XMLHTTP');
            if (postData) {
                req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            }
            req.onreadystatechange = function () {
                if (req.readyState != 4) return;
                if (req.status != 200 && req.status != 304) {
                    alert('Http error ' + req.status);
                    return;
                }
                callback(req);
            };
            if (req.readyState == 4) return;
            req.send(postData);
        }
        
        /*var RetrievalData = function () {};
        RetrievalData.prototype.ajax = sendRequest;
*/
            
        var RetrievalData = {
            ajax: sendRequest
        };
        
        //逃出闭包
        window.Data = RetrievalData;
    })();
posted @ 2011-09-22 00:51  leamiko  阅读(187)  评论(0)    收藏  举报
document.getElementById('MySignature') && document.getElementById('MySignature').style.display = "none"; document.getElementById('MySignature') && document.getElementById('blog_post_info').style.display = "none";