当前位置:网站首页>微信小程序request请求携带cookie,验证是否已登录
微信小程序request请求携带cookie,验证是否已登录
2022-07-25 06:58:00 【追风少年?】
微信小程序设置cookie
微信对wx.request这个API做了修改,小程序发起的请求并不是直接请求我们的后台服务器,而是先通过微信的服务器再发送到我们的后台服务器,既然请求都是要通过微信的服务器发送到我们的服务器,那么我们的服务器也就无从识别请求的发起者了。因此,cookie在微信小程序的后台开发中是无法使用的
因为小程序不支持cookie,所以不能直接使用后台语言的session:
1. 在登录请求成功后读取 返回值的, header的cookie,并本地存储
//登录请求回来之后,读取res的header的cookie
//cookie是个唯一标识
wx.setStorageSync("cookie", res.header["Set-Cookie"])
在下一次请求的时候设置header,并写入cookie
//创建header
var header;
header = {
'content-type': 'application/json',
'cookie':wx.getStorageSync("cookie")//读取cookie
};
//进行请求,一般外层都有一个封装,然后放在公共类里边
wx.request({
url: URL,
method: method,
header: header,//传在请求的header里
data: datas,
success(res) {
//请求成功的处理
}
)}
这样服务器就可以接收到携带的cookies
边栏推荐
- 【transformer】DeiT
- Vscode saves setting configuration parameters to the difference between users and workspaces
- QT actual combat case (53) -- using qdrag to realize the drag puzzle function
- Cointelegraph撰文:依托最大的DAO USDD成为最可靠的稳定币
- JS data type judgment - Case 6 delicate and elegant judgment of data type
- [sword finger offer] analog implementation ATOI
- EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移
- Do you know the same period last year in powerbi
- Addition, deletion, modification and query of DOM elements
- 章鱼网络 Community Call #1|开启 Octopus DAO 构建
猜你喜欢

Do you know the same period last year in powerbi

【C】程序环境和预处理

在C# WinForms应用程序中安装,配置和使用MetroFramework

Easy gene chip SEQ analysis method: practical workflow and advanced applications

【每日一题】1184. 公交站间的距离

A little consideration of strategic mode

HTX00001_ Keil51 bug encountered in serial port parsing

Common mode inductance has been heard many times, but what principle do you really understand?

LeetCode46全排列(回溯入门)
![[Yugong series] July 2022 go teaching course 015 assignment operators and relational operators of operators](/img/37/4a892b96bec8cfa7efe38046c5dfc9.png)
[Yugong series] July 2022 go teaching course 015 assignment operators and relational operators of operators
随机推荐
Rongyun launched a real-time community solution and launched "advanced players" for vertical interest social networking
Easy to use code statistics tool
How to learn C language?
Oracle table creation statement template
Baidu Post Bar crawler gets web pages
[daily question 1] 1184. Distance between bus stops
机器人工程-教学品质-如何判定
Yolov7 model reasoning and training its own data set
【C】程序环境和预处理
[add, delete, modify, and check the array base]
When the graduation season comes, are you ready? What are we going to do
Basic usage of thread class
mvc与三层结构终极区别
Mathematics Olympiad vs Informatics Olympiad (July 19, 2022)
Will eating fermented steamed bread hurt your body
Expandablelistview nested GridView display incomplete problem
knapsack problem
LeetCode46全排列(回溯入门)
探讨影响自动化测试成败的重要因素
Get all file names of the current folder