我很想知道为什么 '%20' 被用作 URL 中的空格,特别是为什么使用 %20 以及为什么我们甚至首先需要它。
最佳答案
它叫做 percent encoding .某些字符不能在 URI 中(例如 #
,因为它表示 URL 片段),因此它们用可以是的字符表示( #
变成 %23
)
这是同一篇文章的摘录:
When a character from the reserved set (a "reserved character") has special meaning (a "reserved purpose") in a certain context, and a URI scheme says that it is necessary to use that character for some other purpose, then the character must be percent-encoded. Percent-encoding a reserved character involves converting the character to its corresponding byte value in ASCII and then representing that value as a pair of hexadecimal digits. The digits, preceded by a percent sign ("%") which is used as an escape character, are then used in the URI in place of the reserved character. (For a non-ASCII character, it is typically converted to its byte sequence in UTF-8, and then each byte value is represented as above.)
空格字符的字符代码是
32
:> ' '.charCodeAt(0)
32
这是
20
在 base-16 中:> ' '.charCodeAt(0).toString(16)
"20"
在它前面加上一个百分号,你会得到
%20
.
关于url - 为什么 '%20' 在 URL 中用作空格的起源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13900835/