小程序需要登陆其他系统,获取到token的数据。
http.postRequest 是将wx.request请求封装在http.js中。
http.js:wx.request头部的信息设置
var header = {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'bearer ' + wx.getStorageSync('token'),
'version': '1.0.0',
}
function request(url, params, method, onSuccess, onFailed) {
wx.request({
url: api + url,
data: dealParams(params),
method: method,
header: header,
success: function (res) {
if (res.statusCode == 200) {
onSuccess(res.data);
} else {
onFailed(res)
}
},
fail: function (error) {
onFailed("");
}
})
}
实例请求方法:
http.postRequest("/token/get", params,
function (res) {
wx.setStorageSync('token', res.data.access_token)
//该接口调用是需要token认证的
this.findByOpenId()
},
function (err) {
});
首次登录小程序,首次缓存数据,this.findByOpenId() 在 wx.setStorageSync 之后并且需要token值,在http.js文件中 wx.getStorageSync('token') 是空值。
你可以这样尝试下
wx.setStorage({ key:"key", data:"value", success:function(res){ // 网络请求 } })
若有帮助请帮忙点个"有用"谢谢↓↓