• ajax 纯javascript


    function createXHR(){
        if(typeof XMLHttpRequest != 'undefined'){
            return new XMLHttpRequest();
        }else if(typeof ActiveXObject != 'undefined'){
            var version = [
                    'MSXML2.XMLHttp.6.0',
                    'MSXML2.XMLHttp.3.0',
                    'MSXML2.XMLHttp',
            ];
            for(var i=0; i<version.length; i++){
                try{
                    return new ActiveXObject(version[i]);
                }catch(e){
    
                }
            }
        }else{
            throw new Error('您的系统或浏览器不支持XHR对象!');
        }
    }
    
    
    //名值对转换字符串
    function params(data){
        var arr=[];
        for(var i in data){
            arr.push(encodeURIComponent(i) + '=' + encodeURIComponent(data[i]));
        }
        return arr.join('&');
    }
    
    //封装Ajax
    function ajax(obj){
        var xhr = new createXHR();
        obj.url = obj.url + '?rand='+ Math.random();
        obj.data = params(obj.data);
        if(obj.method == 'get')obj.url += obj.url.indexOf('?') == -1 ? '?' +obj.data : '&' + obj.data ;
        if(obj.async === true){
            xhr.onreadystatechange=function(){
                if(xhr.readyState == 4){
                    callback();
                }
            }
        }
        xhr.open(obj.method, obj.url, obj.async);
        if(obj.method === 'post'){
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.send(obj.data);
        }else{
            xhr.send(null);
        }
        if(obj.async === false){
            callback();
        }
        function callback(){
            if(xhr.status == 200){
                obj.success(xhr.responseText);
            }else{
                console.log("获取数据错误!错误代号:"+ xhr.status +"错误信息:"+ xhr.statusText);
            }
        }
    }
    
    //调用Ajax
    addEvent(document, 'click', function(){
        ajax({
            method:'get',
            url:'demo.php',
            data:{
                'name':'L&ee',
                'age':100
            },
            success:function(text){
                console.log(text);
            },
            async:true
        });
    })
    //封装Ajax
    function ajax(obj){
        var xhr = (function(){
            if(typeof XMLHttpRequest != 'undefined'){
                return new XMLHttpRequest();
            }else if(typeof ActiveXObject != 'undefined'){
                var version = [
                        'MSXML2.XMLHttp.6.0',
                        'MSXML2.XMLHttp.3.0',
                        'MSXML2.XMLHttp',
                ];
                for(var i=0; i<version.length; i++){
                    try{
                        return new ActiveXObject(version[i]);
                    }catch(e){
    
                    }
                }
            }else{
                throw new Error('您的系统或浏览器不支持XHR对象!');
            }
        })();
        obj.url = obj.url + '?rand='+ Math.random();
        //obj.data = params(obj.data);
        obj.data = (function(data){
            var arr=[];
            for(var i in data){
                arr.push(encodeURIComponent(i) + '=' + encodeURIComponent(data[i]));
            }
            return arr.join('&');
        })(obj.data);
        if(obj.method == 'get')obj.url += obj.url.indexOf('?') == -1 ? '?' +obj.data : '&' + obj.data ;
        if(obj.async === true){
            xhr.onreadystatechange=function(){
                if(xhr.readyState == 4){
                    callback();
                }
            }
        }
        xhr.open(obj.method, obj.url, obj.async);
        if(obj.method === 'post'){
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.send(obj.data);
        }else{
            xhr.send(null);
        }
        if(obj.async === false){
            callback();
        }
        function callback(){
            if(xhr.status == 200){
                obj.success(xhr.responseText);
            }else{
                console.log("获取数据错误!错误代号:"+ xhr.status +"错误信息:"+ xhr.statusText);
            }
        }
    }
    /* 
    * @Author: ocean
    * @Date:   2015-06-22 23:21:37
    * @Last Modified by:   ocean
    * @Last Modified time: 2015-06-22 23:42:10
    */
    
    'use strict';
    
    // 惰性载入
    function createXHR(){
        if(typeof XMLHttpRequest != "undefined"){
            createXHR = function(){
                return new XMLHttpRequest();
            }
        } else if (typeof ActiveXObject != "undefined"){
            createXHR = function(){
                if(typeof arguments.callee.activeXString != "string"){
                    var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"],
                        i, len;
    
                    for(i = 0, len = versions.length; i < len; i++){
                        try{
                            new ActiveXObject(versions[i]);
                            arguments.callee.activeXString = versions[i];
                                break;
                        } catch (ex) {
                            //skip
                        }
                    }
                }
    
                return new ActiveXObject(arguments.callee.activeXString);
            }
        } else {
            createXHR = function(){
                throw new Error("NO XHR object available.");
            }
        }
    
        return createXHR();
    }
    
    
    // 模块模式
    var createXHR = (function(){
        if(typeof XMLHttpRequest != "undefined"){
            return function(){
                return new XMLHttpRequest();
            }
        } else if (typeof ActiveXObject != "undefined"){
            return function(){
                if(typeof arguments.callee.activeXString != "string"){
                    var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"],
                        i, len;
    
                    for(i = 0, len = versions.length; i < len; i++){
                        try{
                            new ActiveXObject(versions[i]);
                            arguments.callee.activeXString = versions[i];
                            break;
                        } catch (ex) {
                            //skip
                        }
                    }
                }
    
                return new ActiveXObject(arguments.callee.activeXString);
            }
        } else {
            return function(){
                throw new Error("NO XHR object available.");
            }
        }
    })();
  • 相关阅读:
    【Java集合】试读LinkedList源码
    【Java集合】试读ArrayList源码
    类加载器ClassLoader的理解
    Spring中Bean的不同配置方式
    Spring中Bean的生命周期
    关于反射的一点理解
    Vector与ArrayList 的理解
    java多态的实现原理(JVM调用过程)(综合多篇文章,参考见文末)
    并发编程的模型分类(转载于https://link.zhihu.com/?target=http%3A//www.54tianzhisheng.cn/2018/02/28/Java-Memory-Model/)强烈推荐!
    Thread线程类
  • 原文地址:https://www.cnblogs.com/oceanden/p/3943927.html
Copyright © 2020-2023  润新知