3

Something I still don't understand when performing an http-get request to the server is what the advantage is in using JS function encodeURIcomponent to encode each component of the http-get.

Doing some tests I saw the server (using PHP) gets the values of the http-get request properly also if I don't use encodeURIcomponent! Obviously I still need to encode at client level the special character & ? = / : otherwise an http-get value like this "peace&love=virtue" would be considered as new key value pair of the http-get request instead of a one single value. But why does encodeURIcompenent encodes also many other characters like 'è' for example which is translated into %C3%A8 that must be decoded on a PHP server using the utf8_decode function.

By using encodeURIcomponent all values of the http-get request are utf8 encoded, therefore when getting them in PHP I have to call each time the utf8_decode function on each $_GET value which is quite annoying.

Why can't we just encode only the & ? = / : characters?

see also: JS encodeURIComponent result different from the one created by FORM It shows that encodeURIComponent does not even encode properly because a simple browser FORM GET encodes characters like '€', in different way. So I still wonder what does this encodeURIComponent is for?

4

2 回答 2

7

That is because

A Uniform Resource Identifier (URI) is defined in [RFC3986] as a sequence of characters chosen from a limited subset of the repertoire of US-ASCII [ASCII] characters.

So officially unicode is not supported; see the RFC for details. All modern browsers support it though, and that is why you get your results just fine.. but for the odd case where some browser or system that does not support it you encode it and make sure it works fine across all standard compliant browsers..

于 2010-02-10T16:40:57.740 回答
5
于 2010-04-11T11:32:34.377 回答