微信小程序不能保存Cookie,導(dǎo)致每次wx.request到服務(wù)端都會(huì)創(chuàng)建一個(gè)新的會(huì)話(huà)(傳過(guò)去的sessionid會(huì)變化),小程序端就不能保持登錄狀態(tài)了。
一個(gè)比較簡(jiǎn)單的辦法就是把服務(wù)端response的Set-Cookie中的值保存到Storage中。
登錄成功后,添加Cookie:
wx.setStorageSync("cookieKey", res.header["Set-Cookie"]);然后調(diào)用接口時(shí),在header中加入:
'Cookie': wx.getStorageSync('cookieKey')接口調(diào)用由之前的:
wx.request({
url: 'test.php',
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
success (res) {
console.log(res.data)
}
})變?yōu)椋?/p>
wx.request({
url: 'test.php',
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' ,
'Cookie': wx.getStorageSync('cookieKey')
},
success (res) {
console.log(res.data)
}
})





