手动封装ajax

(function() {
    xhr = {
        creatXhr: function() {
            return new XMLHttpRequest();
        },
        creatIeXhr: function() {
            return new ActiveXObject("Microsoft.XMLHTTP");
        },
        creatError: function() {
            alert("哪个浏览器");
            return null;
        },
        creatXHR: function() {
            var xhr = null;
            if (window.XMLHttpRequest) {
                this.crearXHr = this.creatXhr;
            } else {
                this.crearXHr = this.creatIeXhr;
            }
            try {
                xhr = this.crearXHr();
            } catch (e) {
                this.crearXHr = this.creatError;
                xhr = this.crearXHr();
            }
            return xhr;
        },
        ajax: function(options) {
            var defaults = {
                type: "get",
                async: true
            };
            var opts = Object.assign({}, defaults, options);
            var xhr = this.creatXHR(),
                method = (opts.type || "GET").toUpperCase(),
                ispost = method == "POST",
                data = this.param(opts.data),
                url = this.buildUrlParm(opts.url, data, ispost);
            xhr.open(method, opts.url, typeof(opts.async) == "undefined" ? true : opts.async);
            if (ispost) {
                xhr.setRequestHeader("content-type", "application/www-form-urlencode");
            }
            var statusChange = this.statusChange;
            xhr.onreadystatechange = function() {
                statusChange(xhr, opts, opts.success, typeof(opts.error) == "undefined" ? false : opts.error);
            }
            xhr.send(ispost ? data : null);
        },
        param: function(data) {
            if (!data) {
                return null;
            }
            if (data.constructor !== "Object") {
                return data;
            }
            var paramArray = [];
            for (var key in data) {
                paramArray.push(key + "=" + data[key]);
            }
            return paramArray.join("&");
        },
        buildUrlParm: function(url, data, isPost) {
            if (!isPost && data) {
                if (url.indexOf("?") == -1) {
                    url += "?" + data;
                } else {
                    url += "&" + data;
                }
            }
            return url;
        },
        statusChange: function(xhr, opts, success, error) {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var result = null;
                    if (opts.dataType == "json") {
                        result = eval("(" + xhr.responseText + ")");
                    } else if (opts.dataType == "xml") {
                        result = xhr.responseXML;
                    } else {
                        result = xhr.responseText
                    }
                    success(result);
                } else {
                    if (error) {
                        error.call(xhr, statusText, status)
                    }
                }
            } else {
                if (error) {
                    error.call(xhr, statusText, status)
                }
            }
        }
    };
    window.ajax = function(opts) {
        xhr.ajax.call(xhr, opts);
    }
})();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值