Tuesday, July 28, 2009

How do I escape an url to asp using xmlhttp and unescape it for a responce?

I am having trouble with xmlhttpobj.open when the I want to send strings to asp including an url containing '%26amp;' and '?' characters. I also need to "unescape" the string back to an url before returning it in a responce using the asp doc. The url also needs to be stored in mysql and sometimes I have trouble adding %26amp;?# characters there as wel.





Thanks for your time.

How do I escape an url to asp using xmlhttp and unescape it for a responce?
I've been having this same issue using javascript to call ASP subpages (AJAX-style).





I use a replacement phrase for characters causing trouble, so in your example I would change "?" to "XqmX" or suchlike. In javascript this would be:





var MyUrl = "APage.asp?AValue=Hello";


MyUrl = MyUrl.replace(/\?/g, "XqmX");





which would change 'MyUrl' to "APage.aspXqmXAValue=Hello". Notice I've escaped the '?' to '\?' as '?' is a regular expression character I believe.





Unescaping it it on the ASP VBScript side would look like this (assuming you passed the 'MyUrl' variable to the ASP page using the same name):





Dim MyUrl


MyUrl = Response.QueryString("MyUrl")


MyUrl = Replace(MyUrl, "XqueX", "?")





Returning it to the pre-escaping version.


This is a bit hacky, but it works.





I hope that contains some information of use!

pink flowers

No comments:

Post a Comment