微信小程序获取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=>{
}
})
}