zoukankan      html  css  js  c++  java
  • 小程序网络请求封装(一)

    //封装接口post  from表单类型
    function way1(url, data, cb) {
      wx.showLoading({})   //加载动画  
      wx.request({
        url: '网络地址' + url,
           //域名 (据情况而定)  url为地址
        data: data,
          //所需要传的参数
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
             // 处理form表单的请求头
          "cache-control": "no-cache"    //处理在回调时反应慢的问题(可不写)
        },
        method: 'POST',
           //方式为post
        dataType: 'json',
        responseType: 'text',
        success: function(res) {  // 请求成功后
          wx.hideLoading();  //请求成功后加载动画结束
          return typeof cb == "function" && cb(res)   // 用来判断返回的是否是函数
        },
    
        fail: function(res) {   //请求失败
          wx.hideLoading();
          wx.showModal({
            title: '网络错误',
            content: '网络出错,请刷新重试',
            showCancel: false
          })
          return typeof cb == "function" && cb(false)
        },
      })
    }
    
    
    //封装接口post    json类型
    function way2(url, data, cb) {
      wx.showLoading({})
      wx.request({
        url: '网络地址' + url,
        data: data,
        header: {
          "Content-Type": "application/json;charset=UTF-8",
          "cache-control": "no-cache"
        },
        method: 'POST',
        dataType: 'json',
        responseType: 'text',
        success: function(res) {
          wx.hideLoading();
          return typeof cb == "function" && cb(res)
        },
        fail: function(res) {
          wx.hideLoading();
          wx.showModal({
            title: '网络错误',
            content: '网络出错,请刷新重试',
            showCancel: false
          })
          return typeof cb == "function" && cb(false)
        },
      })
    }
    
    //封装接口get 基本都是json类型的
    function way3(url, data, cb) {
      wx.showLoading({})
      wx.request({
        url: '网络地址' + url, 
        data: data,
        header: {
          "Content-Type": "application/json;charset=UTF-8",
          "cache-control": "no-cache"
        },
        method: 'GET',
    
        dataType: 'json',
        responseType: 'text',
        success: function(res) {
          wx.hideLoading();
          return typeof cb == "function" && cb(res)
        },
        fail: function(res) {
          wx.hideLoading();
          wx.showModal({
            title: '网络错误',
            content: '网络出错,请刷新重试',
            showCancel: false
          })
          return typeof cb == "function" && cb(false)
        },
      })
    }
    
    //将方法暴露
    module.exports = {
      method1: way1,
      method2: way2,
      method3: way3,
    }
  • 相关阅读:
    BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
    BZOJ 1040: [ZJOI2008]骑士( 树形dp )
    BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )
    HDU 5667 Sequence 矩阵快速幂
    FZU 2225 小茗的魔法阵 扫描线+树状数组
    UVA 11916 Emoogle Grid 离散对数 大步小步算法
    UVA 11754 Code Feat 中国剩余定理+暴力
    FZU 2092 收集水晶 dp+bfs
    FZU2090 旅行社的烦恼 巧妙floyd 最短路
    UVA 11426 GCD
  • 原文地址:https://www.cnblogs.com/xiaozhou223/p/12671343.html
Copyright © 2011-2022 走看看