六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 18|回复: 0

关于encodeURIComponent

[复制链接]

升级  4.67%

62

主题

62

主题

62

主题

举人

Rank: 3Rank: 3

积分
214

最佳新人

 楼主| 发表于 2013-1-23 02:56:41 | 显示全部楼层 |阅读模式
我们通过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");

人,或多或少,总有些自以为很肯定的东西,其实是错误的认知。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表