An error occurred while setting Chinese characters for safari cookies. safaricookie

Source: Internet
Author: User

An error occurred while setting Chinese characters for safari cookies. safaricookie

Recently, H5 was used for mobile phone development. Because it is a Windows operating system, in order to facilitate development and debugging, it was directly tested on the chrome browser and then tested on the android machine, after the function is basically completed, many strange problems occur when running normally applications on android and running on IOS. According to troubleshooting, it is found that related information cannot be obtained because the cookie value is not obtained.

At first, I thought it was a Chinese garbled cookie. Later, I found that the cookie value was not assigned a value. I checked the information online and found that safari does not allow non-ASCII encoding values. In other words: chinese storage is not allowed.

To solve this problem, the cookie value must be encoded before decoding.

When the backend sets the asp.net for cookie and the Javascript used by the front end, can their encoding and decoding be consistent? You can only try it now:

I tried several methods and found that HttpUtility. UrlEncode () was successfully encoded:

1  cookie = new HttpCookie("rdname");2  cookie.Value = HttpUtility.UrlEncode(user.RegisterDeptName);3  cookie.Expires = System.DateTime.Now.AddDays(30);4  context.Response.Cookies.Set(cookie);

Client Javascript decoding:

1 var deptName = cookie('rdname');2 deptName= decodeURIComponent(deptName);3 $("#pickerlb").val(deptName);

That is to say, decodeURIComponent () in Javascript is paired with HttpUtility. UrlEncode () in C.

 

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: info-contact@alibabacloud.com 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.