微信小程序获取response header中的字段以及在request header中传值给后台 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 小程序相关 >

微信小程序获取response header中的字段以及在request header中传值给后台

发表时间:2020-9-30

发布人:葵宇科技

浏览次数:254

1.微信小程序中获取request header的方法如下:


const requestTask = wx.request({
  url: rootDocment + $api.home.porList,
  data: {},
  method:'get',
  header: {
    'Content-Type':'application/x-www-form-urlencoded',
    Authorization: token
  },
  success(res) {
    console.log(res.data)
  }
})
requestTask.onHeadersReceived((res) => {
  console.log('onHeadersReceived: ', res)
  console.log('获取到了header')
})

2.微信小程序中请求头中添加信息

    header: option.header || {
      'Content-Type': 'application/json',
      'Authorization':wx.getStorageSync('token')
    },

3.封装请求

function getReq(option) {
  option = {
    url: option.url || "",
    port: option.port || "user",
    data: option.data || {},
    header: option.header || {
      'Content-Type': 'application/json',
      'Authorization':wx.getStorageSync('token')
    },
    success: typeof option.success == "function" && option.success || function () { 
    },
    fail: typeof option.fail == "function" && option.fail || function () {
    },
    complete: typeof option.complete == "function" && option.complete || function () { },
    method: option.method || "get"
  }
    const requestTaskPost = wx.request({
      url: baseUrl + _port[option.port] + option.url,
      method: option.method,
      data: option.data,
      header: option.header,
      success: function (res) {
        getApp().globalData.iserror = false;
        return option.success(res.data);
      },
      fail: function (res) {
        getApp().globalData.iserror = true;
        return option.fail(res.data);
      },
      complete: function (res) {
        //断网页面的显示和隐藏
        let pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        if (currPage!== undefined){
          currPage.setData({
            iserror: getApp().globalData.iserror
          })
          return option.complete(res.data)
        }
      }
    })
    requestTaskPost.onHeadersReceived((res) => {
      //判断是否有用户Token,如果有放在缓存中
      if(res && res.header && res.header.Authorization){
        wx.setStorageSync('token',res.header.Authorization)
      }
    })
}

function postReq(option) {
  option = {
    url: option.url || "",
    port: option.port || "user",
    data: option.data || {},
    header: option.header || {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization':wx.getStorageSync('token')
    },
    success: typeof option.success == "function" && option.success || function () {
    },
    fail: typeof option.fail == "function" && option.fail || function () {

    },
    complete: typeof option.complete == "function" && option.complete || function () { },
    method: option.method || "post"
  }
    const requestTaskGet =  wx.request({
      url: baseUrl + _port[option.port] + option.url,
      header: option.header,
      data: option.data,
      method: option.method,
      success: function (res) {
        getApp().globalData.iserror = false;
        return option.success(res.data)
      },
      fail: function (res) {
        getApp().globalData.iserror = true;
        return option.fail(res.data)
      },
      complete: function (res) {
        //断网页面的显示和隐藏
        let pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        if (currPage !== undefined){
          currPage.setData({
            iserror: getApp().globalData.iserror
          })
          return option.complete(res.data)
        }
      }
    })
    requestTaskGet.onHeadersReceived((res) => {
      //判断是否有用户Token,如果有放在缓存中
      if(res && res.header && res.header.Authorization){
        wx.setStorageSync('token',res.header.Authorization)
      }
    })
}

module.exports = {
  get: getReq,
  post: postReq,

}


使用该请求的方法

import $ from '../../../utils/request.js';

  getFun:function(){
    $.get({
      url:'',
      port:'',
      data,
      success:res=>{
      }
    })
  },
  postFun:function(){
     $.get({
      url:'',
      port:'',
      data,
      success:res=>{
      }
    })
  }
  
  

相关案例查看更多