safari cookie sets the resolution for Chinese failure

  • 2020-06-23 02:02:56
  • OfStack

Use H5 for mobile client development recently, because it is window operating system, in order to facilitate the development and debugging, test on chrome browser directly, then the mobile end in android machine test, when after the completion of the basic functions, the original run on android normal application, run on IOS, appeared a lot of strange questions, according to the screen, found to be due to cookie did not take to the value information not available.

1 At first, I thought it was cookie's confused Chinese code. Later, I tracked it and found that the value of cookie was not assigned successfully. I checked the data online and found that safari did not allow the value with non-ASCII encoding.

To solve this problem, you must first encode the cookie value when setting it, and then decode it when setting the value.

The back end sets asp.net for cookie and the front end gets Javascript for Javascript. Can their encoding and decoding be 1? Currently, I can only try 1:

Several methods were tried, and it was found that HttpUtility. UrlEncode() was used to encode successfully:


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

Client Javascript is decoded as:


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

That is: decodeURIComponent() in Javascript is paired with HttpUtility.UrlEncode () in C#.


Related articles: