【发布时间】:2010-02-10 16:34:11
【问题描述】:
在向服务器执行http-get请求时我仍然不明白的是使用JS函数encodeURIcomponent对http-get的每个组件进行编码的好处是什么。
做一些测试,我看到 服务器(使用 PHP)如果我不使用 encodeURIcomponent 也能正确获取 http-get 请求的值! 显然,我仍然需要在客户端级别对特殊字符 & 进行编码? = / :否则像“peace&love=virtue”这样的http-get值将被视为http-get请求的新键值对,而不是单个值。 但是为什么 encodeURIcompenent 还要对许多其他字符进行编码,例如 'è' 被翻译成 %C3%A8 必须在 PHP 服务器上使用 utf8_decode 函数解码。
通过使用 encodeURIcomponent,http-get 请求的所有值都是 utf8 编码的,因此在 PHP 中获取它们时,我每次都必须在每个 $_GET 值上调用 utf8_decode 函数,这非常烦人。
为什么我们不能只编码 & ? = / : 字符?
另见:JS encodeURIComponent result different from the one created by FORM 它表明 encodeURIComponent 甚至没有正确编码,因为简单的浏览器 FORM GET 以不同的方式对像“€”这样的字符进行编码。所以我还是想知道这个 encodeURIComponent 是干什么用的?
【问题讨论】:
标签: php javascript encodeuricomponent