反应路线参数自动解码字符串 [英] React routes param auto decode string
问题描述
react router params 会自动解码字符串吗?
Hi does react router params automatically decodes string?
例如我有这条路线
<Route path="/callback/:url"/>
当我用 C5jb20%3D
当我 console.log() 参数时,它会打印 C5jb20=
it will print C5jb20=
when I console.log() that parameters
这是预期的行为吗?我可以改变这种行为,以便我仍然得到编码的字符串吗?
Is this an expected behavior? Can I change this behavior so that I still get the encoded string?
谢谢!
推荐答案
C5jb20%3D
是一个 URL 编码字符串,它是一种对 URL 中的特殊字符进行编码的方法.例如,如果您有一个像 ?redirect=/some/path
这样的 URL 参数,服务器可能会将正斜杠解释为路由路径,并弄乱预期的路由.这就是为什么您会在 URL 中看到诸如 ?redirect=%2Fsome%2Fpath
和您的示例之类的内容.
C5jb20%3D
is a URL encoded string, which is a way to encode special characters in a URL. For example, if you have a URL parameter like ?redirect=/some/path
, a server could interpret the forward slashes as routing paths, and mess up the expected routing. That's why you see things like ?redirect=%2Fsome%2Fpath
, and your example, in URLs.
window.location.path
将具有原始的、未编码的 URL.
window.location.path
will have the original, unencoded URL.
您也可以使用 encodeURIComponent
:
You could also get it back with encodeURIComponent
:
encodeURIComponent('C5jb20=') // "C5jb20%3D"
不过,您可能不想这样做,具体取决于您的用例.
You probably don't want to, though, depending on your use case.
这篇关于反应路线参数自动解码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!