随笔 - 315  文章 - 1  评论 - 12  阅读 - 24万

ajax封装

复制代码
function ajaxPost(data, url, successFn, errorFn,beforeFn,completeFn) {
    $.ajax({
        url: globe.baseUrl + url,
        data: data,
        dataType: "json",
        type: "POST",
        beforeSend:function(){
            if(typeof beforeFn == "function"){
                beforeFn();
            }
        },
        complete:function(){
            if(typeof completeFn == "function"){
                completeFn();
            }
        },
        success: function (d) {
            if (d.code === globe.statusCode.success_code) {
                successFn(d);
            } else if (d.code === globe.statusCode.overdue_token) {
                layer.msg("登录失效,请重新登录", {time: 2000},function () {
                    removeStorage('userInfo');
                    openLoginWindow();
                });

            } else {
                layer.msg(d.msg, {time: 2000});
                successFn(d); // code为0的回调
            }
        },
        error: function (e) {
            console.log(e);
            layer.msg("服务器请求失败,稍后重试!", {time: 2000});
            typeof errorFn == "function" && errorFn(e);
        }
    });
}
复制代码

调用

复制代码
  let layer_index = 0;
ajaxPost(_param, globe.baseApi.school_index, function (res) {
   // 成功回调
},function () {     // 失败回调 },function () {
    // 接口结果返回之前回调 layer_index
= layer.msg('数据搜索中...',{ icon: 16 }); },function () {
    // 接口请求结束回调 layer.close(layer_index); });
复制代码

 

posted on   小虾米吖~  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2022-02-14 jquery.dataTables实现table自定义列显示隐藏
2019-02-14 easyui 布局之window和panel一起使用时,拉动window宽高时panel不跟随一起变化
2019-02-14 easyui组件window拖动时超过浏览器顶部则无法拖回

点击右上角即可分享
微信分享提示