base文件 ajax 封装post的方法

方法一

postData: function (params) {
      var postParm = {
        murl: '', // 请求url
        mdata: {}, // 请求参数
        timeout: 30000, // 请求超时时间,默认30秒
        successCallBack: '' // 成功回调函数
      }
      postParm = $.extend(postParm, params);
      // mdata = mdata ? mdata : "";
      // if (typeof mdata == "object") {
      //   for (var a in mdata) {
      //     if (typeof mdata[a] == "string") {
      //       mdata[a] = mdata[a].replace(/(^\s*)|(\s*$)/g, "");
      //     }
      //   }
      // }
      var options = {
        type: "POST",
        timeout: postParm.timeout,
        url: base.baseURL + postParm.murl,
        dataType: "json",
        data: postParm.mdata
      }
      return $.ajax(options).then(function (response) {
        if (response.status == 400) {
          base.notification({
            type: 'error',
            message: response.msg,
            duration: 3000
          });
          setTimeout(function () {
            window.location.href = base.baseURL + "login.jsp";
          }, 3000);
          return false;
        }

        if (response.status == 300) {
          base.notification({
            type: 'error',
            message: response.msg,
            duration: 3000
          });
          setTimeout(function () {
            window.location.href = base.baseURL + "index/index.jsp";
          }, 3000);
          return false;
        }

        if (response && response.length > 0 || response && response.success) {
          postParm.successCallBack ? postParm.successCallBack() && base.notification({
            message: response.msg,
            duration: 3000,
            title: '成功提示'
          }) : '';
          return response;
        }
        if (response && response.success == false) {
          if (response.message || response.msg) {
            base.notification({
              type: 'error',
              message: response.msg || response.message,
              duration: 3000
            });
          }
          return response
        }
        return response
      });
    },

用法

 base.postData({
                    murl: 'sys/user/list',//接口
                    mdata: {//参数
                        pageNumber: 1,
                        pageSize: 1
                    }
                }).then(function (res) {
                
  });

方法二

postJson: function (murl, mdata, successCallBack, errCallback) {
      // mdata = mdata ? JSON.stringify(mdata) : "";
      var options = {
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        type: "POST",
        timeout: 30000,
        url: base.baseURL + murl,
        dataType: "json",
        data: mdata,
        success: function (e) {
          if (e.status == 400) {
            base.notification({
              type: 'error',
              message: e.msg,
              duration: 3000
            });
            setTimeout(function () {
              window.location.href = base.baseURL + "login.jsp";
            }, 3000);
            return false;
          }

          if (e.status == 300) {
            base.notification({
              type: 'error',
              message: e.msg,
              duration: 3000
            });
            setTimeout(function () {
              window.location.href = base.baseURL + "index/index.jsp";
            }, 3000);
            return false;
          }
          if (e && e.length > 0 || e && e.success) {
            successCallBack ? successCallBack(e) : function () {}
          } else {
            errCallback ? errCallback(e) : base.notification({
              type: 'error',
              message: e.msg,
              duration: 3000
            });
          }
        },
        error: function (xhr, type, errorThrown) {
          errCallback ? errCallback() : base.notification({
            type: 'error',
            message: type == 'timeout' ? '请求超时' : '请求异常',
            duration: 3000
          });
        }
      };
      $.ajax(options);
    },

用法

base.postJson('infoSta/devCallRefusal', { // 拒绝接受,通知设备显示“总台繁忙,请稍后重试”
          devCode: data.devCode
        }, function (result) {
          console.log("send devCallRefusal success");
        }, function (err) {
        
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值