最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to use decodeURIComponent in asp? - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Dec 1, 2011 at 15:25 Joel Coehoorn 416k114 gold badges578 silver badges813 bronze badges asked Feb 18, 2010 at 9:56 AlexAlex 1,9739 gold badges36 silver badges41 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

i think you mean you want to decode the URI ponent in the vb 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

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

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
%>

When you AJAX/Post a normal "text" bee with "unsafe" characters, so you have to encodeURI to send some "text" like ments 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
%>

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: 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.

发布评论

评论列表(0)

  1. 暂无评论