美文网首页
ajax封装js

ajax封装js

作者: 风之伤_3eed | 来源:发表于2017-10-19 20:27 被阅读0次

函数封装的是代码,采用面向对象的形式封装

(function () {

暴露出ajax接口

ajax = window.ajax = {}

增加1个方法: get方法

参数
obj 是1个对象,代表参数的集合,里面有很多参数,如下所示:
url 请求的地址与参数
data 发送的数据
async 是否是同步, true / false
fail 失败的处理回调函数
success 成功的处理回调函数

ajax.get = function(obj) {
        if (obj.async == undefined) {
        obj.async = true;
    }
    if (obj.data == undefined) {
        obj.data = "";
    }
    var xhr = new XMLHttpRequest();
    var url = obj.url;
    url += "?"
    url += obj.data;
    xhr.open("GET", url, obj.async);
    xhr.send()
    if (obj.async == true) {
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var str = xhr.responseText;
                    obj.success && obj.success(str);
                } else {
                    obj.fail && obj.fail();
                }
            }   
        }
    } else {
        // 同步方式
        if (xhr.status == 200) {
            var str = xhr.responseText;
            obj.success && obj.success(str);
        } else {
            obj.fail && obj.fail();
        }
    }
    
}

增加1个方法: post方法

参数:
obj 是1个对象,代表参数的集合,里面有很多参数,如下所示:
url 请求的地址与参数
data 发送的数据
async 是否是同步, true / false
fail 失败的处理回调函数
success 成功的处理回调函数

ajax.post = function(obj) {
    if (obj.async == undefined) {
        obj.async = true;
    }
    if (obj.data == undefined) {
        obj.data = "";
    }
    var xhr = new XMLHttpRequest();
    var url = obj.url;
    xhr.open("GET", url, obj.async);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(obj.data)
    if (obj.async == true) {
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var str = xhr.responseText;
                    obj.success && obj.success(str);
                } else {
                    obj.fail && obj.fail();
                }
            }
        }
    } else {
        // 同步方式
        if (xhr.status == 200) {
            var str = xhr.responseText;
            obj.success && obj.success(str);
        } else {
            obj.fail && obj.fail();
        }
    }
}
})();
gamersky_02origin_03_20179917265C7.jpg

相关文章

网友评论

      本文标题:ajax封装js

      本文链接:https://www.haomeiwen.com/subject/fsupuxtx.html