zoukankan      html  css  js  c++  java
  • 微信小程序写一个请求接口函数

    var getAjax = function(options){
      wx.getNetworkType({   //获取网络类型
        success: function (res) {
          // 返回网络类型, 有效值:
          // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
          if(res.networkType == 'none'){
            showModal("网络已断开,请联网后重试!",function(){
              if(options.complete){
                options.complete();
              }
            });
          }else{
            if (wx.showLoading) {
              wx.showLoading({
                title: '加载中',  
              })
            }
            if (options.token) {
              var _header = {
                'content-type': 'application/json',
                'Cookie': 'JSESSIONID=' + options.token
              };
            } else {
              var _header = {
                'content-type': 'application/json'
              }
            }
            return wx.request({
              url: baseUrl + options.url,
              data: options.params,
              method: options.method ? options.method : 'GET',
              success: options.success,
              header: _header,
              fail: function (res) {
                if (options.fail) {
                  options.fail(res)
                }
              },
              complete: function () {
                if (wx.hideLoading) {
                  setTimeout(function () {
                    wx.hideLoading()//关闭提示
                  }, 400);
                }
                if (options.complete) {
                  options.complete();
                }
              }
            });
          }
        },
        fail:function(res){
          console.log("获取网络状态失败:",res);
        }
      });
    }
     
    showModal(cnt,fn);为自己写的一函数,用于显示弹窗,cnt为要显示的内容,fn为点击确定按钮执行的函数,有则执行。
    if (wx.hideLoading)与if(wx.showLoading) 用于判断是否支持这个api
  • 相关阅读:
    114DNS Public DNS+ 阿里DNS 百度DNS 360 DNS派 Google DNS公共DNS评测体验报告
    无需任何软件!斐讯K2 V22.x.xx.xx版本刷机教程(2016年11月更新)
    Gmail技巧之无限别名
    XP系统中,系统属性→计算机名中,网络ID是灰色的不可用状态,还有下面的“更改”按钮点进去,“隶属于”的域和工作组也是灰色不可用
    winForm窗体的close、dispose
    Taro的学习1
    react-redux的学习
    IOS 单页面状态栏文字白色,其余黑色
    IOS连续退出多个界面(回退到指定界面)
    iPhone真机调试iPhone is not available.Pleasere connect the device问题
  • 原文地址:https://www.cnblogs.com/apgy/p/7727767.html
Copyright © 2011-2022 走看看