关于encodeURIComponent

我们通过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");

人,或多或少,总有些自以为很肯定的东西,其实是错误的认知。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值