I am trying to pass special characters as querystring in url as part of GET request. I am constructiong that string in javascript function.
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?
Please let me know.
Thanks.
I am trying to pass special characters as querystring in url as part of GET request. I am constructiong that string in javascript function.
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?
Please let me know.
Thanks.
Share Improve this question edited Jun 16, 2013 at 9:42 nawfal 73.2k59 gold badges336 silver badges376 bronze badges asked Jun 16, 2013 at 9:35 GaneshGanesh 531 gold badge1 silver badge3 bronze badges 1- What language are we talking about? The tags say c# and javascript, your question says java and the code can be either c# or javascript... – venerik Commented Jun 16, 2013 at 9:56
2 Answers
Reset to default 13You should url encode it using the encodeURIComponent
function:
var queryString =
"list=" + encodeURIComponent("ABC-48+12") +
"&level=" + encodeURIComponent("first");
This function will take care of properly encoding your query string parameter values:
list=ABC-48%2B12&level=first
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');