简体   繁体   中英

Javascript: encodeURIComponent returns diffrent value between the value typed in keyboard and value pasted from clipboard

I'm facing the problem with the URL encoding when sending the search request with the query.

My query accepts the UTF-8 characters from the input form element.

But the value encoded is very different if I try with the encodeURIComponent.

Eg:

  1. I typed this word "nhầm" from the keyboard and it returns: encodeURIComponent("nhầm") => "nh%E1%BA%A7m"
  2. I copied this word "nhầm" then pasted it into and it returns: encodeURIComponent("nhầm") => "nh%C3%A2%CC%80m"

Could someone explain?

The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

In other words, it replaces special characters with certain characters that can be used in the URL.

So you need to decode the encoded URI from the server-side.

Node.js:
decodeURI();

PHP:
urldecode();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM