我有一个序列化的字符串,我正在将其发送到服务器,如下所示:
counter=1&Id=4&type=2332&amount=3232&gstIncluded=3232&paymentMethod=2¬es=2332#fdsf&docId=0&ref=3232&isEdit=true
我在这里面临的问题是用户添加的注释中有“#”符号。这导致查询字符串中断,并且我在 Controller 中的 # 之后没有收到数据。甚至 docId、ref 和 isEdit 都被跳过了。
谁能告诉我更好的解决方案。
我试过对注释字段进行 html 转义,但它没有转义 #。
最佳答案
#
是 URI(统一资源标识符)中需要编码的字符,因为它通常用于显示已选择/指向哪个 ID。
您可能会在地址栏中的 URI 末尾看到一个 #
,它后面可能是页面上元素的 ID - 例如 Lodash 文档( _.uniqBy
for例如。您可以使用内置的 encodeURIComponent
函数对其进行编码。编码后,它看起来像 %23
:
console.log(encodeURIComponent("#"));
您也可以用这种方式对完整的 URI 进行编码:
console.log(encodeURIComponent("counter=1&Id=4&type=2332&amount=3232&gstIncluded=3232&paymentMethod=2¬es=2332#fdsf&docId=0&ref=3232&isEdit=true"));
关于javascript - 转义序列化字符串中的#符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575915/