结果分类-Ajax之三

Ajax中的x指的是XML,早期的多少格式都爱好用XML,然后一少有的分析。当然不难的也会再次来到HTML(或称HTML片段)。
当今应用JSON格式的也很多。依照分化须求,给Ajax对象添加了三个实用方法:Ajax.text、Ajax.json、Ajax.xml。

Ajax.text重回纯文本,即responseText

Ajax.json返回json,即会将responseText解析成js对象

Ajax.xml返回xml文档,即responseXML

运用形式与Ajax.request相同,第三个参数是呼吁url,第一个是布署参数。

一体化代码

Ajax = 
function(){
    function request(url,opt){
        function fn(){}
        var async   = opt.async !== false,
            method  = opt.method    || 'GET',
            type    = opt.type      || 'text',
            encode  = opt.encode    || 'UTF-8',
            data    = opt.data      || null,
            success = opt.success   || fn,
            failure = opt.failure   || fn;
            method  = method.toUpperCase();
        if(data && typeof data == 'object'){//对象转换成字符串键值对
            data = _serialize(data);
        }
        if(method == 'GET' && data){
            url += (url.indexOf('?') == -1 ? '?' : '&') + data;
            data = null;
        }   
        var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
        xhr.onreadystatechange = function(){
            _onStateChange(xhr,type,success,failure);
        };
        xhr.open(method,url,async);
        if(method == 'POST'){
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=' + encode);
        }
        xhr.send(data);
        return xhr; 
    }
    function _serialize(obj){
        var a = [];
        for(var k in obj){
            var val = obj[k];
            if(val.constructor == Array){
                for(var i=0,len=val.length;i<len;i++){
                    a.push(k + '=' + encodeURIComponent(val[i]));
                }               
            }else{
                a.push(k + '=' + encodeURIComponent(val));
            }               
        }
        return a.join('&');
    }
    function _onStateChange(xhr,type,success,failure){
        if(xhr.readyState == 4){
            var s = xhr.status, result;
            if(s>= 200 && s < 300){
                switch(type){
                    case 'text':
                        result = xhr.responseText;
                        break;
                    case 'json':
                        result = function(str){
                            return (new Function('return ' + str))();
                        }(xhr.responseText);
                        break;
                    case 'xml':
                        result = xhr.responseXML;
                        break;
                }
                success(result);    
            }else{
                failure(xhr);
            }
        }else{}
    }
    return (function(){
        var Ajax = {request:request}, types = ['text','json','xml'];
        for(var i=0,len=types.length;i<len;i++){
            Ajax[types[i]] = function(i){
                return function(url,opt){
                    opt = opt || {};
                    opt.type = types[i];
                    return request(url,opt);
                };
            }(i);
        }
        return Ajax;
    })();
}();

 

相关:

ajax_03.zip

https://github.com/snandy/io

 

相关文章