zoukankan      html  css  js  c++  java
  • 小程序 请求request, ajax封装

    let baseUrl= 'https://www.baidu.com'
    function request (url, method, data, head, dataType, doSuccess, doFail, doComplete) {
      let header = {};
      if (head == 0) {
        header = { 
            'Content-Type': 'appliaction/json',
            'cookie': wx.getStorageSync('cookieKey'),
          }
      } else if (head == 1) {
        header = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'cookie': wx.getStorageSync('cookieKey'),
          }
      }

      wx.request({
        url: baseUrl+ url,
        data: data,
        header: header,
        method: method,
        dataType: dataType,
        responseType: 'text',
        success: (res) => {
          if (typeof doSuccess == "function") {
            doSuccess(res);
          }
        },
        fail: (res) => {
          if (typeof doFail == "function") {
            doFail(res);
          }
        },
        complete: (res) => {
          if (typeof doComplete == "function") {
            doComplete(res);
          }
        },
      })
    };

    let baseUrl2= 'https://www.baidu2.com'
    function ajax(url, method, data, head, dataType, doSuccess, doFail, doComplete) {
      let header = {};
      if (head == 0) {
        header = {
          'Content-Type': 'appliaction/json',
          'cookie': wx.getStorageSync('cookieKey'),
        }
      } else if (head == 1) {
        header = {
          'Content-Type': 'application/x-www-form-urlencoded',
          'cookie': wx.getStorageSync('cookieKey'),
        }
      }
      wx.request({
        url: baseUrl2+ url,
        data: data,
        header: header,
        method: method,
        dataType: dataType,
        responseType: 'text',
        success: (res) => {
          if (typeof doSuccess == "function") {
            doSuccess(res);
          }
        },
        fail: (res) => {
          if (typeof doFail == "function") {
            doFail(res);
          }
        },
        complete: (res) => {
          if (typeof doComplete == "function") {
            doComplete(res);
          }
        },
      })
    };

    module.exports = {
      request: request,
      ajax: ajax
    }




  • 相关阅读:
    angular学习笔记(二十五)-$http(3)-转换请求和响应格式
    angular学习笔记(二十四)-$http(2)-设置http请求头
    angular学习笔记(二十三)-$http(1)-api
    Replace 在动态sql中的实现
    SQL 中 replace 替换字符串中的字符 ''
    sqlserver 时间字符串转化为时间格式
    update set where exists 多表更新 在mssql、MySQL、Oracle中分别用法
    LPAD在Oracle中和 mssql以及在MySQL中的不同用法 以及调用存储过程方法
    支付宝对账单下载Java正式商户调用
    支付宝对账单下载Java沙箱调用
  • 原文地址:https://www.cnblogs.com/xushan03/p/15156201.html
Copyright © 2011-2022 走看看