Development of micro-credit enterprise number of micro-letter attendance cookies use _javascript tips

Source: Internet
Author: User

In the article to introduce the micro-letter enterprise development of micro-letter attendance Baidu map positioning, and then through the study of micro-letter enterprise development of micro-letter attendance cookies Use, the specific contents are as follows.

Using micro-credit attendance, each use of micro-credit enterprise development: the relationship between micro-credit user Information and Web page session the inside method, call the micro-letter interface, a bit slow, micro-letter official also recommended the use of cookies, but how to use cookies, they have not been clear.

Originally thought in the server to obtain the client's data have two methods, one is to put the query string on the URL, one is placed in the form, post submitted, I have used before, but mainly in the client use, can never put the data in cookies directly to the service side, Even if there is a hidden field in the form by reading the data from the cookie, then post it to the server.

Obviously micro-credit attendance is actually a URL, in the process of entering the URL, there is no post data process. Only after entering the URL and then submitting via user, or Ajax submission. In short, it seems impossible to directly submit the data in cookies directly to the server. Seems to be in deadlock. So I studied cookies once again, and found that cookies appear to be submitted to the server, but it's not the same location as post, and of course I didn't find the documentation, the test found. As long as you set cookies, each entry URL will be submitted to the cookies, you can naturally read the value of cookies on the server. Then really understand how to remember the true principle of the password. Not to read the value of cookies, put the hidden fields, and then through the Ajax submitted to the server, you can be free of landing.

Cookies can be seen on the service side, SessionID also through the way of cookies to the service side.

Front-end JS read, set Cookies method:

function Setcookie (name, value) {///two parameters, one is the name of the cookie, one is the value 
 var days = 30;//This cookie will be saved 30 day 
 var exp = new Date () ; New Date ("December, 9998"); 
 Exp.settime (Exp.gettime () + days * * * * 1000); 
 Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring (); 
function GetCookie (name) {//Fetch cookies function   
 var arr = document.cookie.match (New RegExp ("(^|)" + name + "= ([^;] *)(;|$)")); 
 if (arr!= null) return unescape (arr[2)); return null; 

CSharp service-side operation Cookies:

Set cookies

HttpCookie cookies = new HttpCookie ("Usercode", username); 
   Cookie. Expires = DateTime.Now.AddDays (a);//(365 * 3600);   
   This. Response.appendcookie (cookie); 
   HttpCookie Cookiedeviceid = new HttpCookie ("DeviceId", Rt. DEVICEID); 
   Cookiedeviceid.expires = DateTime.Now.AddDays (10);//(365 * 24 * 3600);   

Read cookies:

HttpCookie Tthttpcookie = this. Request.Cookies.Get ("Usercode"); 
HttpCookie Tthttpcookiedeviceid = this. Request.Cookies.Get ("DeviceId"); 
String code = request.querystring["Code"]; 
if (Tthttpcookie = null | | tthttpcookiedeviceid = null) 
 Weiapi (code); 
else { 
 string username = Tthttpcookie.value; 
 string DeviceId = Tthttpcookiedeviceid.value; 
 if (string. IsNullOrEmpty (username) | | String. IsNullOrEmpty (DeviceId)) 
  Weiapi (code); 
 else { 
  new appexception ("Read cookies usercode=" + username + ", deviceid=" + DeviceId); 
  Initsession (username, DeviceId); 

The above content to introduce the micro-letter enterprise development of micro-letter attendance Cookies Use, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.