wx.request请求服务器, 如果服务端下发多个Set-Cookie会导致cookie错误
https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
这是服务端下发的Response
wx.request 返回的 response.header['Set-Cookie']结果如下
JSESSIONID=6D9A6F819C765192BB22224EFF383891; Path=/api; HttpOnly,user_token=637780ebdbaf677625a1c061833a965fdd366431219fae14ce3xxxxxxf8d5; Domain=.gelonghui.com; Expires=Sat, 20-Mar-2021 17:40:45 GMT; Path=/
如果用Nodejs常用的cookie模块解析如下
user_token 变成了HttpOnly,user_token
2 回复
//index.js
//获取应用实例
const app = getApp()
const jssha1 = require("../../miniprogram_npm/miniprogram_npm/js-sha1/index.js")
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
// 綁定點擊事件
bindHi() {
// 登錄
wx.login({
success(sr) {
// 獲取加密數據
wx.getUserInfo({
success(rres){
const code = sr.code;
const timestamp = ~~(+new Date() / 1000);
const sign = jssha1(`${timestamp}${code}wxxxxxxxxxxxxxx9ee417aa35ad230`);
// 我們系統的接口Set-cookie了兩次
wx.request({
url: 'https://api.xxx.com/we-chat/mini-program/session',
method: "POST",
header:{
platform: "xxxx"
},
data: {
code,
sign,
timestamp,
iv:rres.iv,
encryptedData: rres.encryptedData
},
success(r){
console.log(r.header['Set-Cookie'])
}
},)
}
})
}
})
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse){
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
},
getUserInfo: function(e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
})