Home > Web Front-end > HTML Tutorial > Safari cookie setting failed in Chinese

Safari cookie setting failed in Chinese

WBOY
Release: 2016-08-04 08:53:14
Original
1282 people have browsed it

  Recently, H5 was used for mobile development. Since it is a window operating system, in order to facilitate development and debugging, the test was conducted directly on the Chrome browser, and then the mobile test was conducted on the Android machine. When the function was basically completed, it was originally run on Android. When a normal application is run on IOS, many strange problems occur. According to the investigation, it is found that the relevant information cannot be obtained because the cookie does not have a value.

 At first, I thought it was a problem with Chinese garbled characters in the cookie. Later, I found out that the value of the cookie was not assigned successfully at all. I checked the information online and found that Safari does not allow non-ASCII encoded values. In other words: Chinese characters are not allowed to be stored.

 In order to solve this problem, the cookie value must be encoded first, and then decoded when the value is obtained.

 The back-end uses asp.net to set cookies, and the front-end uses Javascript to obtain it. Can their encoding and decoding be consistent? Currently I can only give it a try:

I tried several methods and found that encoding with HttpUtility.UrlEncode() was successful:

<span style="color: #008080;">1</span>  cookie = <span style="color: #0000ff;">new</span> HttpCookie(<span style="color: #800000;">"</span><span style="color: #800000;">rdname</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008080;">2</span>  cookie.Value =<span style="color: #000000;"> HttpUtility.UrlEncode(user.RegisterDeptName);
</span><span style="color: #008080;">3</span>  cookie.Expires = System.DateTime.Now.AddDays(<span style="color: #800080;">30</span><span style="color: #000000;">);
</span><span style="color: #008080;">4</span>  context.Response.Cookies.Set(cookie);
Copy after login

 Client-side Javascript is decoded as:

<span style="color: #008080;">1</span> <span style="color: #0000ff;">var</span> deptName = cookie('<span style="color: #800000;">rdname</span>'<span style="color: #000000;">);
</span><span style="color: #008080;">2</span> deptName=<span style="color: #000000;"> decodeURIComponent(deptName);
</span><span style="color: #008080;">3</span> $("#pickerlb").val(deptName);
Copy after login

That means: decodeURIComponent() in Javascript and HttpUtility.UrlEncode() in C# are paired.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template