我们通过ajax提交数据的时候,常常会用encodeURIComponent把参数包装一遍。
原先以为encodeURIComponent是你页面是什么编码,就会按该编码方式编码数据,但这是错的。
在ECMAScript里找到关于encodeURIComponent的说明如下:
When a character to be included in a URI is not listed above or is not intended to have the special meaning sometimes given to the reserved characters, that character must be encoded. The character is first transformed into a sequence of octets using the UTF-8 transformation, with surrogate pairs first transformed from their UCS-2 to UCS-4 encodings. (Note that for code points in the range [0,127] this results in a single octet with the same value.) The resulting sequence of octets is then transformed into a string with each octet represented by an escape sequence of the form "% xx".
于是,ajax提交中文的编码解决方案:
1.用utf-8编码方式读取传递参数
2.返回的response用utf-8编码
3.设置response header编码方式,如php下可以如此设置:header("Content-type:text/xml;charset=utf-8");
人,或多或少,总有些自以为很肯定的东西,其实是错误的认知。
原先以为encodeURIComponent是你页面是什么编码,就会按该编码方式编码数据,但这是错的。
在ECMAScript里找到关于encodeURIComponent的说明如下:
When a character to be included in a URI is not listed above or is not intended to have the special meaning sometimes given to the reserved characters, that character must be encoded. The character is first transformed into a sequence of octets using the UTF-8 transformation, with surrogate pairs first transformed from their UCS-2 to UCS-4 encodings. (Note that for code points in the range [0,127] this results in a single octet with the same value.) The resulting sequence of octets is then transformed into a string with each octet represented by an escape sequence of the form "% xx".
于是,ajax提交中文的编码解决方案:
1.用utf-8编码方式读取传递参数
2.返回的response用utf-8编码
3.设置response header编码方式,如php下可以如此设置:header("Content-type:text/xml;charset=utf-8");
人,或多或少,总有些自以为很肯定的东西,其实是错误的认知。