Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass special characters as query string in url

Tags:

javascript

c#

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.

like image 572
Ganesh Avatar asked Jun 16 '13 09:06

Ganesh


1 Answers

You 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 
like image 97
Darin Dimitrov Avatar answered Sep 27 '22 22:09

Darin Dimitrov