How to save server sessionid on Mini Program
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.