如何在url中将特殊字符作为查询字符串传递
I am trying to pass special characters as querystring in url as part of GET request. I am constructiong that string in javascript function.
我试图在URL中传递特殊字符作为查询字符串作为GET请求的一部分。我在javascript函数中构造该字符串。
var queryString = "list=ABC-48+12&level=first";
Then I append the string to url as part of request which goes to struts action class. In the action class I get the "list" value as "ABC-48 12"
, the "+"
character is not passed. How to pass special characters in the string as part of url and get back in the java class?
然后我将字符串附加到url作为请求的一部分,该请求转到struts动作类。在动作类中,我将“list”值设为“ABC-48 12”,不传递“+”字符。如何在字符串中传递特殊字符作为url的一部分并返回java类?
Please let me know.
请告诉我。
Thanks.
2 个解决方案
#1
-2
You need to use a regular expression with the global option set as the first parameter instead of a string: (in regular expressions "+" is a special character, so we have to escape it with a backslash.)
你需要使用一个正则表达式,将全局选项设置为第一个参数而不是字符串:(在正则表达式中,“+”是一个特殊字符,所以我们必须用反斜杠转义它。)
safeQueryString = safeQueryString.replace(/+/g, '%2B');
safeQueryString = safeQueryString.replace(/ + / g,'%2B');
更多相关文章
- JSON.parse(xhr.responseText)意外的字符串错误
- JS计算任意字符串宽度
- 如何在JavaScript中按大写和小写分割字符串?
- 使用jackson json将属性添加到json字符串
- ajax请求返回一个空字符串作为响应
- 在Javascript中将带有空格的字符串数组转换为小写,然后在Webkit中
- 如何将带有双引号反斜杠的JSON字符串转换为Javascript对象
- Javascript正则表达式应该允许空和非空字符串,但它不会
- 获取拆分字符串数组的最后一个元素