ajax二次封装(自用)

1,406 阅读1分钟

基于jquery的ajax的二次封装

基于jquery weui jqweui.cn/

function show_loading() {
    $.showLoading("加载中");
}
function hide_loading() {
    $.hideLoading();
}
var loadingCount = 0
function add_loadingCount() {
    if (loadingCount == 0) {
        show_loading();
    }
    loadingCount++;
}
function reduce_loadingCount() {
    if (loadingCount <= 0) return;
    loadingCount--;
    if (loadingCount === 0) {
        hide_loading();
    }
}
//封装ajax
function request(url, type, data, successfn, errorfn, completefn){
    add_loadingCount();
    type = (type==null || type=="" || typeof(type)=="undefined")? "post" : type;
    data = (data==null || data=="" || typeof(data)=="undefined")? {} : data;
    $.ajax({
        url: url,
        dataType: "json",
        data: data,
        type: type,
        success: function (res) {
            successfn(res);
        },
        error: function (res) {
            errorfn(res)
        },
        complete: function () {
            // completefn();
        },
    });
}

调用:

    var url = "http://www.microsoft.com";
    var data = "";
    request(url, "", data, function(data){

    },function(data){

    })