举例:
在小程序开发当中,需要获取手机验证码,在点击获取验证码的时候获取响应头部Set-Cookie,在注册调接口的时候设置为头部带过去,才能够确保是同一个验证码
因为小程序开发中我们需要获取到后端给的cookie进行请求验证,但是微信并没有帮我们保存cookie,那么我们要维持会话需要自己来保存cookie,并且请求的时候加上cookie
// 获取请求头Set-Cookie
wx.request({
url: url,
data: { },
method:"get",
header: {
'content-type': 'application/json', // 默认值
},
success: function(res) {
console.log(res.header["Set-Cookie"]) // 请求头
wx.setStorageSync("sessionid", res.header["Set-Cookie"]) // 存储本地
}
})
请求头
image.png
wx.request({
url: url,
data: { },
method:"get",
header: {
'content-type': 'application/json',
'cookie': wx.getStorageSync("sessionid") // 读取
},
success: function(res) { }
})
设置请求头
image.png
可能出现的问题:
在小程序开发工具中使用的是set-cookie,可以获得,但是在手机端获得不了,是因为手机端的话,必须首字母大写Set-Cookie,才可以获得,这是小程序的一个bug
网友评论