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
    }




  • 相关阅读:
    Jupyter Notebook 编辑器美化
    Python3 与 C# 基础语法对比(List、Tuple、Dict、Set专栏)
    Ubuntu 18.04 安装微信(附企业微信)
    区块链概念
    Python3 与 C# 基础语法对比(String专栏)
    用Python3、NetCore、Shell分别开发一个Ubuntu版的定时提醒(附NetCore跨平台两种发布方式)
    ArchLinux 设置时间同步和硬件时间同步错误 No usable clock interface found
    ArchLinux dwm的安装和配置
    POJ-1182 食物链 并查集(互相关联的并查集写法)
    POJ-2236 Wireless Network 并查集
  • 原文地址:https://www.cnblogs.com/xushan03/p/15156201.html
Copyright © 2011-2022 走看看