ajax.js

//Ajax
$mq.ajax({url:"http://localhost/ajax/ajax.php",
    //async:true,
    type:"get",
    // cache:true,
    data:{name:"aaa",age:18},
    success:function(res){
        console.log(res);
    },
    error:function(error){
        console.log("error:",error);
    }
});
//jsonp
$mq.jsonp({
    url:url,
    callback:"aaa",
    cbName:"callback",
    success:function(res){
        console.log(res)
    }
});
;!function(a,name) {
    "use strict";
    var c = function(options){
        this.options = {
            url:location.href,
            async:true,
            success:null,
            error:null,
            type:"get",
            data:"",
            contentType:"application/x-www-form-urlencoded",
            cache:false
        };
        //替换默认值
        for(var i in this.options){
            if(i in options){
                this.options[i] = options[i];
            }
        }
        this.ajaxHttp();
    },
    d = function(options){
        this.options = {
            url:"",
            callback:"jsonp",
            cbName:"callback",
            success:null
        };
        this.init(options);
    };
    //ajax
    c.prototype.ajaxHttp = function(type){
        //处理data
        this.setData();
        //转换大写判断是GET/POST
        this.options.type = this.options.type.toLocaleUpperCase();
        if(this.options.type == "GET"){
            if(this.options.data){
                this.options.url = this.options.url.indexOf("?") == -1 ? this.options.url + "?" + this.options.data : this.options.url + "&" + this.options.data;
            }
            if(this.options.cache){
                if(this.options.url.indexOf("?") == -1){
                    this.options.url = this.options.url + "?_t=" + new Date().getTime();
                }else{
                    this.options.url = this.options.url + "&_t=" + new Date().getTime();
                }
            }
            this.xhRequest(null);
        }else if(this.options.type == "POST"){
            this.xhRequest(this.options.data);
        }
    },
    //xhr
    c.prototype.xhRequest = function(send){
        var that = this,
            xhr = new XMLHttpRequest;
        xhr.open(this.options.type,this.options.url,this.options.async);
        var oPromise = new Promise(function(resolve,reject){//
            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                        resolve(xhr.responseText);//that.options.success(xhr.responseText);
                    }else{
                        reject(xhr.responseText);
                    }
                }
            }
        });//Promise
        if(this.options.type == "POST"){
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        }
        xhr.send(send);
        oPromise
            .then(function(res){
                that.options.success(res);
            })
            .catch(function(res){
                that.options.error(res);
            });
    },
    //处理data
    c.prototype.setData = function(){
        //拼接参数
        if(this.options.data){
            var str = "";
            for(var j in this.options.data){
                str += j + "=" + this.options.data[j] + "&";
            }
            this.options.data = str.slice(0,str.length-1);
        }
    };
    //jsonp-替换默认值,
    d.prototype.init = function(options){
        for(var i in this.options){
            if(i in options){
                this.options[i] = options[i];
            }
        }
        if(!(this.options.cbName in this.options)){
            this.options[this.options.cbName] = options[this.options.cbName];
            delete this.options.callback;
        }
        this.urlSplice();
    },
    d.prototype.urlSplice = function(){
        //拼接url

        if(this.options.url.indexOf("?") == -1){
            this.options.url += "?" + this.options.cbName + "=" + this.options[this.options.cbName];
        }else{
            this.options.url += "&" + this.options.cbName + "=" + this.options[this.options.cbName];
        }
        this.options.url += "&_=" + new Date().getTime();
        this.addWindow();
    },
    d.prototype.addWindow = function(){
        var that = this;
        //添加返回执行函数
        window[this.options[this.options.cbName]] = function(success){
            that.options.success(success);
        }
        this.addScript();

    },
    d.prototype.addScript = function(){
        //添加srcipt便签
        var scriptTag = document.createElement("script");
        scriptTag.src = this.options.url;
        document.body.appendChild(scriptTag);
    };
    a[name] = {
        v: "0.0.1",
        //ajax
        ajax(options){
            var b = new c(options);
            return b;
        },
        jsonp(options){
            var b = new d(options);
            return b;
        }
    }
} (window,'$mq');

你可能感兴趣的:(ajax.js)