您不能也不应该试图阻止特殊字符被转换。正确的方法是在发送用户之前故意对它们进行编码,然后在读取数据之前对其进行解码。如果您使用的是 javascript,那么您可以通过encodeURIComponent(string)和执行此操作decodeURIComponent(string)。
encodeURIComponent()将接受任何字符串,并将其转换为对 URL 友好的格式。
decodeURIComponent()将采用转换后的字符串,并将其更改回常规格式。
至于你不应该试图阻止它们被转换的原因......我将在这里引用 Mozilla:
For example, if a user writes "Jack & Jill", using encodeURIComponent the text will get encoded as "Jack %26 Jill". Without encoding, the ampersand could be interpreted on the server as the start of a new field and jeopardize the integrity of the data.
换句话说,假设您在 URL 中允许使用符号,并且有人创建了一个名为“ Jack&baseApi=evilURL ”的用户名。您的网址最终会是
http://localhost:4200?username=Jack& baseApi=evilURL & baseApi =https://someOtherService
而且我不想站在你的服务器上,试图猜测正确的 baseApi 是“evilURL”还是“someOtherService”。