tags:

views:

131

answers:

4

I am trying to pass a string via HTTP request which has one of the character as % in the URL query string.

url = url + "?q=" + str + "&block=" + block;  // str contains the '%' character

But on the ColdFusion page where I'm sending this information is returning following error:

Element Q is undefined in URL.

Could you please tell me that how can I encode the % sign in a URL?

+6  A: 

You should url-encode all the values you are passing as query parameters, but the url-encoding for % is %25

Update: if you're constructing the query parameters in javascript, you probably want to do:

url=url+"?q="+encodeURIComponent(str)+"&block="+encodeURIComponent(block)

(Updated again with ZeissS' very helpful suggestion to use encodeURIComponent instead of escape. See also http://xkr.us/articles/javascript/encode-compare/)

Brad G.
You should use `encodeURIComponent()`, since you are escaping a single component of the URL.
ZeissS
thanks a lot its working now :)
Deepak Yadav
+9  A: 

Pass your string trough the function encodeURI(...) it will escape all the special characters not only the %

Mic
Thanks a lot!!! encodeURI() was a great help..
Deepak Yadav
pst
Mic
Thanks I returned here to ask tht only bcoz I was getting problems with ) thank you
Deepak Yadav
+3  A: 

%25

http://www.w3schools.com/TAGS/ref_urlencode.asp

BojanG
Thank you for your time.
Deepak Yadav
A: 

URLEncodedFormat() in ColdFusion

Henry
Thank you for your time. Can I use URLEncodedFormat() while I am inserting the data in Oracle database ?
Deepak Yadav
sure, but you must ask yourself if that's really what you want to do.
Henry