encodeURI and encodeURIComponent

Source: Internet
Author: User

  Turned from ZCCST and fell into encodeURIComponent's hole again.

Problem:

Ajax.get (url+ '? K1 ' =v1+ ' &k2 ' =v2+ ' &k3 ' =v3, ...);

Since the URL is only encodeURI encoded, the parameter you want to commit has Searchword = A+b, which becomes a B.

Workaround:

Ajax.post (URL, params,....)

Where the params is an object.

Cause: If the parameter is an object or an array, the component library has encodeuricomponent for you. And if you're just a string, the component library doesn't do anything, so the special characters are filtered to the back end.

encodeURI () usage

The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().

The purpose of this method is to fully encode the URI, so the encodeURI () function is not escaped for the following ASCII punctuation mark with a special meaning in the URI:;/?:@&=+$,#

Tip : If the URI component contains delimiters, such as? and #, each component should be encoded using the encodeURIComponent () method.

Example:

document.write (encodeURI ("http://www.w3school.com.cn") + "<br/>")  document.write (encodeURI(" Http://www.w3school.com.cn/My first/"))  document.write (encodeURI (",/?:@&=+$# "     )) // annotation: Only the part after the domain name is converted, and the/?:@&=+$ #不处理.    // http://www.w3school.com.cn   // http://www.w3school.com.cn/My%20first/   // ,/?:@&=+$#  

The decodeURI () function decodes a URI encoded by the encodeURI () function.

encodeURIComponent () usage

The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().

Other characters (such as:;/?:@&=+$,# These punctuation marks used to separate the URI component) are replaced by one or more hexadecimal escape sequences.

tip : Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of a URI (such as a protocol, hostname, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.

Example:

document.write (encodeURIComponent ("http://www.w3school.com.cn"))  document.write (encodeURIComponent ( "http://www.w3school.com.cn/p 1/"))  document.write (encodeURIComponent (",/?:@&=+$#"))       // Compare   // http%3a%2f%2fwww.w3school.com.cn   // http%3a%2f%2fwww.w3school.com.cn%2fp%201%2f   // %2c%2f%3f%3a%40%26%3d%2b%24%23  

The decodeURIComponent () function decodes the URI encoded by the encodeURIComponent () function.

encodeURI and encodeURIComponent

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.