JS Code and Asp.net code

Source: Internet
Author: User

1. The window. escape () and HttpUtility. UrlEncodeUnicode () encoding formats are the same: encode a Chinese character in % uxxxx format
Characters not encoded by window. escape include: @ _-. */+, which is inconsistent with the interpretation on http://www.w3school.com.cn/js/jsref_escape.asp.

 

2. The window. encodeURIComponent () and HttpUtility. UrlEncode () encoding formats are the same: encode a Chinese character in the format of % xx

The characters that will not be encoded by window. encodeURIComponent include :'()*-._! ~ This is in line with the interpretation of http://www.w3school.com.cn/js/jsref_encodeuricomponent.asp

Characters not encoded by HttpUtility. UrlEncode include :'()*-._! In comparison, HttpUtility. UrlEncode is one more ~ than window. encodeURIComponent ~ Encoding

 

3. The characters not encoded by window. encodeURI are :-_.! *();/? : @ & =$, #, Compared with encodeURIComponent, it is found that encodeURI is incorrect :;/? : @ & =+ $, # These punctuation marks used to separate URI components for encoding

 

Differences between Asp. Net encoding and JS encoding:

1. The characters not encoded by HttpUtility. UrlEncodeUnicode are the same as those not encoded by HttpUtility. UrlEncode, while the unencoded characters of escape and encodeURIComponent are different.

2. HttpUtility. UrlEncode and HttpUtility. UrlEncodeUnicode are encoded with/, while escape and encodeURIComponent are encoded with/, while encodeURI is not encoded /.

3. HttpUtility. UrlEncode () and HttpUtility. UrlEncodeUnicode () encode spaces as +, while escape, encodeURIComponent, and encodeURI both encode spaces as % 20

 

Use ajax to submit a string:
1. xmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
Var postStr = "val = {name: 'test', age: 19 }";
XmlHttp. send (postStr );

The client sends the following request:
POST/index. aspx HTTP/1.1
Accept :*/*
Accept-Language: zh-cn
Referer: http: // localhost.: 3910/Default. aspx
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0 ;. net clr 2.0.50727 ;. net clr 3.0.04506.648 ;. net clr 3.5.21022; CBA ;. net clr 3.0.20.6.2152 ;. net clr 3.5.30729; baiduie8)
Host: localhost.: 3910
Content-Length: 29
Connection: Keep-Alive
Pragma: no-cache

Val = {name: 'test', age: 19} // If no code is found, the code is directly sent in binary format.

In the Server index. when a breakpoint is set in aspx, the Request is found. form: val = % 7 bname % 3a '% u6885 % u5c0f % u4f1f' % 2 cage % 3a19% 7d (escape encoding is used here) Request. form [0. form ["val"] retrieves "{name: 'test', age: 19 }"

2. xmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
Var postStr = window. encodeURIComponent ("val = {name: 'test', age: 19 }");
XmlHttp. send (postStr );

The client sends the following request:
POST/index. aspx HTTP/1.1
Accept :*/*
Accept-Language: zh-cn
Referer: http: // localhost.: 3910/Default. aspx
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0 ;. net clr 2.0.50727 ;. net clr 3.0.04506.648 ;. net clr 3.5.21022; CBA ;. net clr 3.0.20.6.2152 ;. net clr 3.5.30729; baiduie8)
Host: localhost.: 3910
Content-Length: 59
Connection: Keep-Alive
Pragma: no-cache

Val % 3D % 7 Bname % 3A '% E6 % A2 % 85% E5 % B0 % 8F % E4 % BC % 9f' % 2 Cage % 3A19% 7D // found window. encodeURIComponent Encryption

In the Server index. when a breakpoint is set in aspx, the Request is found. form: val % 3d % 7 bname % 3a '% u6885 % u5c0f % u4f1f' % 2 cage % 3a19% 7d (escape encoding is used here instead of encodeURIComponent encoding ), use Request. form [0] retrieves the value "val = {name: 'test', age: 19}" and uses Request. form ["val"] returns null (this is because the client will encode = % 3d when sending the request. If you use window. encodeURI can retrieve the Request. form ["val"]: "{name: 'test', age: 19)

 

Summary: if you do not use get or post, as long as you use the default value of form's enctype attribute application/x-www-form-urlencoded, the values to be passed through window. encodeURIComponent () encoding and re-transmission (except for spaces containing values, it is not encoded as % 20, but encoded as + ). after being uploaded to the Server, you can use the Server. urlDecode. However, whether it is get or post, enctype is application/x-www-form-urlencoded or multipart/form-data. Use asp.net to view the Request in the background. queryString and Request. in Form, the Chinese characters are converted into the escape encoding format, for example, Request. form =__ VIEWSTATE = % %%2bl0lztpprs7qnr4qmrf4ktw % 3d % 3d & mm = % u6556 % u5fb7 % u8428 % u7684 (English letters are not encoded, some symbols are encoded with encodeURIComponent and escape, such as =, $, etc ).

Why encodeURIComponent instead of escape?
The escape method does not encode character +. We know that in the form field submitted by the user, if there is a space, it will be converted to a + character, and the server will think that the + number represents a space during parsing. Due to this deficiency, the escape method cannot correctly process all non-ASCII characters. Instead, you should try to avoid using the escape method. Instead, you 'd better choose the encodeURIComponent () method.

 

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.