Understanding Encodeuricomponent,encodeuri (for as3.0)

Source: Internet
Author: User
Tags first string rfc

Turn from: http://blog.csdn.net/ljb2010_/archive/2010/12/13/6073507.aspx

Coding is often used in AJAX development, especially in Chinese. So what did Encodeuri,encodecomponent do? Why are there encodeURI, and encodeuricomponent?

Knowledge URI (Uniform Resource Identifier) Reference RFC3968 http://gbiv.com/protocols/uri/rfc/rfc3986.html

URI translation as a Uniform Resource identifier, tentatively understood as the only representation of resources, where we are familiar with the URL, and the urn is a subset of them. About urn Reference Http://www.ietf.org/rfc/rfc2141.txt, here does not explain, did not contact too understand.

Syntax composition of URIs

URI = scheme ":" Hier-part ["?" query] ["#" fragment]

A URI will contain

Scheme (such as HTTP, FTP, etc., is defined by [BCP35]),
Authority [UserInfo @] host [": Port]" There is only one host in the common URL, not before or after. Landing ftp with the @ symbol appears in the
Path path
query string, often in the url "?" The latter section (optional)
Fragment anchor points, which are often in the URL (optional) later in the   "#"
These parts are called components of the URI (Uricomponent), and a URI is made up of many uricomponent, There are uricomponent between the two, which are composed of reserved characters. Understanding these uricomponent, it is good to understand encodeURIComponent and encodeURI.

      foo://example.com:8042/over/there?name=ferret#nose
      /_/  /______________//_________//_________//__/
       |            |             |            |         |
    scheme     authority       path         query   fragment
       |    _____________________|__
     ///                        /
       Urn:example:animal:ferret:nose

For example, when Ajax communication, the content of the resource is as follows a JSON format {url:http://www-zorder.cn?map=1&debug=1,name: "Z-order"}, the request is initiated with ajax_get, if not encoded, There is a get data error, because the & symbol is received at the webserver end when "&" is a delimiter, does not represent the data, so the JSON data must be encoded. This time you should use encodeURIComponent (), because this JSON appears in the query data segment and can be understood as part of the query component in the URI of the GET request, so you should choose to encode the URI component encodeURIComponent, encodeURIComponent will return a valid URI component. Here the legal, refers to the legal character, does not contain reserved characters.

Reserved characters in the URI have "? # [ ] @ ! $ & ' () * +,; =/", (part of double quotation marks). Currently encodeuricomponent in JavaScript will be in addition to reserved characters! Characters other than the () * ' "are encoded.

Summarize:

encodeURI () is the encoding of a uniform resource identifier, such as a common URL connection with Chinese, which recognizes reserved characters as delimiters for URIs.

encodeURIComponent () encodes a URI component that does not recognize reserved characters in the URI as delimiters, but recognizes them as data, as part of the URI component, which encodes the reserved characters, or can be interpreted as escaping

The two encodings are the same, and the encoded data format is:%hex (e.t.%51%b6), where HEX is the hexadecimal representation of the ASCII code for the character. For UTF8 encoded Chinese, such as "Medium" => "%e4%b8%ad", the text in UTF8 is three bytes, the encoded value is the hexadecimal value of each byte, and server-side scripting is easy to decode.

Comparison

The difference between encodeURI and escape is that both are coded and what is the difference.

Reference to Mozilla doc

Http://developer.mozilla.org/En/Core_JavaScript_1.5_Guide/Predefined_Functions/Escape_and_unescape_Functions

The escape function returns the hexadecimal encoding of the argument in the ISO Latin character set.

Escape encoding is to encode the passed-in characters into a Latin character set and return its hexadecimal representation. It actually returns the Unicode UCS-2 encoded hexadecimal value (%uxxxx, 0-127 and ASCII encoding the same%xx), while the UCS-2 encoding takes two bytes and can represent a maximum of 60,000 characters, which is obviously an error in dealing with a remote Chinese word. So in Mozilla's doc above it is pointed out that escape is not recommended for encoding because escape has a limited set of coded characters and is recommended to be encoded using encodeURIComponent.

About the processing of PHP correspondence

The corresponding processing in PHP is like this, PHP's UrlDecode series method can handle the encodeURI results. PHP for JS escape encoding, there is no built-in method to support, but the conversion is very simple, the first string/%u?/d+/match out, and then pack, get the UC2 encoded characters, if the output is UTF8 or gb2312, it needs to be converted and then output.


--------------------------------------------------------------------------------

W3school's explanation:

Http://www.w3school.com.cn/js/jsref_encodeURI.asp

Http://www.w3school.com.cn/js/jsref_encodeURIComponent.asp

Both encodeURI and encodeuricomponent do not encode ASCII letters and numbers, and do not encode these ASCII punctuation marks:-_. ! ~ * ' ().

For the/?:@&=+$,# symbols in these URLs, encodeURI is not encoded and encodeuricomponent is encoded.

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.