Both the encodeURI and encodeURIComponet functions are used in javascript to encode the URI and convert the relevant parameters into data in the UTF-8 encoding format. During URI locating and redirection, non-ASCII codes such as Chinese and Japanese in the parameter are all encoded and converted. Both encodeURI and encodeURIComponet functions are used in javascript to encode the URI, converts relevant parameters to data in UTF-8 encoding format. When URI is redirected to a location, non-ASCII codes such as Chinese and Japanese in the parameter are all encoded and converted.
The functions of these two functions are similar, but there are some differences.
EncodeURI: there are 82 unencoded characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z
EncodeURIComponent: 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, a-z, A-Z
Problem:
Ajax. get (url + '? K1 '= v1 +' & k2 '= v2 +' & k3 '= v3 ,...);
Because the URL is only encoded with encodeURI, the parameter to be submitted is changed to a B When searchWord = a + B.
Solution:
Ajax. post (url, params ,....)
Params is an object.
Cause: If the parameter is an object or array, the component library will encodeURIComponent for you. If you are only a string and the component library does nothing, filter out the special characters at the backend.
EncodeURI () Usage
This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().
The purpose of this method is to fully encode the URI. Therefore, the encodeURI () function will not escape the following ASCII punctuation marks with special meanings in the URI :;/? : @ & =+ $ ,#
Tip: If the URI component contains a separator, such? And #, use the encodeURIComponent () method to encode each component.
Example:
Document. write (encodeURI ("http://www.w3school.com.cn") +"
") Document. write (encodeURI (" http://www.w3school.com.cn/My first/") document. write (encodeURI (",/? : @ & =+ $ # ") // Annotation: only the part after the domain name is converted, and ,/? : @ & =+ $ # Do not process. // Http://www.w3school.com.cn // http://www.w3school.com.cn/My%20first //,/? : @ & =+ $ #
The decodeURI () function can decode the URI encoded by the encodeURI () function.
EncodeURIComponent () Usage
This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().
Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate URI components) are all replaced by one or more hexadecimal escape sequences.
Tip: note the differences between the encodeURIComponent () function and the encodeURI () function. The former assumes that its parameters are part of the URI (such as the protocol, host name, path, or query string ). Therefore, the encodeURIComponent () function uses escape characters to separate the punctuation marks of each part 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 (",/? : @ & = + $ #")) // Comparison // 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 can decode the uri of the encodeURIComponent () function.