Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to save server sessionid on Mini Program

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge of this "how Mini Program saves server-side sessionid" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how Mini Program saves server-side sessionid" article.

Ordinary Web development, is to save the sessionid in cookie transfer.

Whether it is java or php, the server will add Set-Cookie to the header of response

Response HeadersContent-Type:application/json;charset=UTF-8Date:Mon, 02 Apr 2018 16:02:42 GMTSet-Cookie:JSESSIONID=781C7F500DFA24D663BA243A4D9044BC;path=/yht;HttpOnly browser request will also be added to header

Through this sessionid, Request HeadersAccept:*/*Accept-Encoding:gzip, deflate, and brAccept-Language:zh-CN,zh;q=0.8Cache-Control:no-cacheConnection:keep-aliveContent-Length:564content-type:application/jsonCookie:JSESSIONID=781C7F500DFA24D663BA243A4D9044BC;path=/yht;HttpOnly can maintain a conversation between the browser and the server, and keep the browser in the login state.

However, WeChat Mini Programs cannot save the Cookie, which causes a new session to be created every time the wx.request goes to the server, and the mini program cannot keep the login state.

The simple processing methods are as follows:

1. Save the value in the Set-Cookie of the server response to Storage

Wx.request ({

Url: path

Method:method

Header: header

Data:data

Success:function (res) {

If (res & & res.header & & res.header ['Set-Cookie']) {

Wx.setStorageSync ('cookieKey', res.header [' Set-Cookie']); / / Save Cookie to Storage

}

}

Fail:fail

})

Wx.request then takes the Cookie out of Storage and encapsulates it into header

Let cookie = wx.getStorageSync ('cookieKey')

Let path=conf.baseurl+url

Let header = {}

If (cookie) {

Header.Cookie=cookie

}

Wx.request ({

Url: path

Method:method

Header: header

Data:data

Success:success

Fail:fail

})

The above is about "Mini Program how to save the server sessionid" of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report