Alex
Alex

Reputation: 1963

how to use decodeURIComponent in asp?

From my javascript i try to post data to my asp page using encodeURIComponent

var dd = encodeURIComponent(document.getElementById("Remarks").innerHTML);

How i decode my encodeURIComponent in asp page using vbscript?

hoping your support

Upvotes: 4

Views: 15936

Answers (4)

Frobozz
Frobozz

Reputation: 233

For those of us decoding AJAX multipart/form-data, the "built-in" decoding of normal form processing is unavailable. Likewise, @Diego's and @Valerio's Server.CreateObject("ScriptControl") solution, while I am sure it worked at one time, throws a 500 error on modern IIS.

Server-side javascript functions are available to vbscript in Classic ASP. You simply have to declare them in a server-side javascript script block: enter image description here This is actual working file upload code running on IIS 10/Server 2019.

Keep in mind that decodeURI/decodeURIComponent are not panacea. You will likely need to handle quotation (both single and double) as well as ampersands depending on your specific implementation. This solution ONLY addresses accessing javascript from Classic ASP.

Upvotes: 0

Valerio Gentile
Valerio Gentile

Reputation: 1100

I guess you need this: Classic ASP URLDecoder function using decodeURIComponent

<%
FUNCTION URLDecoder(str)
'// This function:
'// - decodes any utf-8 encoded characters into unicode characters eg. (%C3%A5 = å)
'// - replaces any plus sign separators with a space character
'//
'// IMPORTANT:
'// Your webpage must use the UTF-8 character set. Easiest method is to use this META tag:
'// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
'//
    Dim objScript
    Set objScript = Server.CreateObject("ScriptControl")
    objScript.Language = "JavaScript"
    URLDecoder = objScript.Eval("decodeURIComponent(""" & str & """.replace(/\+/g,"" ""))")
    Set objScript = NOTHING
END FUNCTION
%>

Upvotes: 2

Pankaj Kumar
Pankaj Kumar

Reputation: 1768

i think you mean you want to decode the URI component in the vb.net code behind and not vb script.

the thing here is you don't have to it...Request.Querystring("query_string_variable") automatically does it for you.

if you explicitly want to do it you can use

HttpUtility.UrlDecode() in .net

if you want to do it in VBscript , see the answer by Valerio

Upvotes: 8

When you AJAX/Post a normal "text" become with "unsafe" characters, so you have to encodeURI to send some "text" like comments in a textarea

var URL = "somepage.asp";
var Params = "text=Hello World!";
var ajax = getHTTPObject();     
ajax.open("POST", URL, true);
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
ajax.setRequestHeader("Content-length", Params.length);
ajax.setRequestHeader("Connection", "close");
ajax.onreadystatechange = function() { 
    if (ajax.readyState == 4 && ajax.status == 200) {
        divResponse.innerHTML = ajax.responseText; //alert(ajax.responseText);
    } 
}
ajax.send(Params);

The result will be some like:

HelloWorld!

so, in order to encode an URL you have to send it encoded with JavaScript method

var URL = "somepage.asp";
var Params = encodeURI("text=Hello World!");
var ajax = getHTTPObject();     
ajax.open("POST", URL, true);
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
ajax.setRequestHeader("Content-length", Params.length);
ajax.setRequestHeader("Connection", "close");
ajax.onreadystatechange = function() { 
    if (ajax.readyState == 4 && ajax.status == 200) {
        divResponse.innerHTML = ajax.responseText; //alert(ajax.responseText);
    } 
}
ajax.send(Params);

Then the result will be some like:

Hello World!

So the problem is how to "decode" that URI encoded in order to use it in an ASP Classic serverpage

EDIT:

<%
FUNCTION URLDecode(str)
    Dim objScript
    Set objScript = Server.CreateObject("ScriptControl")
    objScript.Language = "JavaScript"
    URLDecode = objScript.Eval("decodeURIComponent(""" & str & """.replace(/\+/g,"" ""))")
    Set objScript = NOTHING
END FUNCTION
%>

Upvotes: 0

Related Questions