js封装get请求

//get请求后台服务
function GetCallService(method, getData, callbackfun, loadingText, async = true) {
    jQuery.support.cors = true;
    var strData=objTostr(getData);
    var url = Common.WebApiUrl + method+'?'+strData;
    //loading
    if (loadingText.length > 0) {
        layer.load(0, { //icon支持传入0-2
            shade: [0.8, 'white'], //0.5透明度的灰色背景
            content: loadingText,
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '39px',
                    'width': '200px',
                    'color': 'black'
                });
            }
        });
    }
    $.ajax({
        type: 'get',
        url: url,
        async: async, //false表示同步
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (responseText) {
            if (loadingText.length > 0) {
                layer.closeAll('loading');
            }
            callbackfun(responseText);
        },
        error: function (XMLHttpRequest, status, errorThrown) {
            if (loadingText.length > 0) {
                layer.closeAll('loading');
            }
            if (status == 'timeout') {
                layer.alert('请求超时,请检查网络或者稍候再试');
            } else {
                layer.alert('请求错误,错误信息:' + status);
            }
        }
    });
}
function objTostr(obj) {
    var res = [];
    for (var key in obj) {
        res.push(encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]));
    }
    return res.join("&");

}

 

posted @ 2022-08-04 16:08  青兰柳  阅读(534)  评论(0编辑  收藏  举报