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.