ajax的js中url传送特殊字符和中文汉字的乱码解决

escape():

The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."

encodeURI():

The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned.

The encodeURI method does not encode the following characters: ":", "/", ";", and "?".

Use encodeURIComponent to encode these characters.

encodeURIComponet():

The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component

When to use which?

The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming and the fact that this function fails to handle non-ASCII characters correctly, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().

escape() will not encode: @*/+

Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs  as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.

encodeURI() will not encode: ~!@#$&*()=:/,;?+' Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs. encodeURIComponent() will not encode: ~!*()' 对比 javascript url编码 escape() 方法： 采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码（xx等于该字符在字符集表里面的编码的16进制数字）。比如，空格符对应的编码是%20。 不会被此方法编码的字符： @ * / + encodeURI() 方法：把URI字符串采用UTF-8编码格式转化成escape格式的字符串。 不会被此方法编码的字符：! @ #$& * ( ) = : / ; ? + '

encodeURIComponent() 方法：把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比，这个方法将对更多的字符进行编码，比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话，不能用这个方法来进行编码，否则 / 字符被编码之后URL将显示错误。

是因为 Java 中的 request.getParameter(String) 方法会进行一次 URI 的解码过程，调用时内置的解码过程会导致乱码出现。
而 URI 编码两次后， request.getParameter(String) 函数得到的是原信息 URI 编码一次的内容。
接着用 java.net.URLDecoder.decode(String str,String codename) 方法，将已经编码的 URI 转换成原文。

• 本文已收录于以下专栏：

举报原因： 您举报文章：ajax的js中url传送特殊字符和中文汉字的乱码解决 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)