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

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=>{
      }
    })
  }
  
  

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值