如何在asp中使用decodeURIComponent?

时间:2010-02-18 09:56:16

标签: javascript asp-classic

从我的javascript尝试使用encodeURIComponent

将数据发布到我的asp页面
var dd = encodeURIComponent(document.getElementById("Remarks").innerHTML);

我如何使用vbscript在asp页面中解码我的encodeURIComponent?

希望得到您的支持

3 个答案:

答案 0 :(得分:7)

我认为你的意思是你想要在后面的vb.net代码中解码URI组件而不是vb脚本。

这里的事情是你没有... Request.Querystring("query_string_variable")自动为你做。

如果您明确要这样做,可以使用

.net

中的

HttpUtility.UrlDecode()

如果您想在VBscript中执行此操作,请参阅Valerio的答案

答案 1 :(得分:2)

我想你需要这个: Classic ASP URLDecode function using decodeURIComponent

<%
FUNCTION URLDecode(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"
    URLDecode = objScript.Eval("decodeURIComponent(""" & str & """.replace(/\+/g,"" ""))")
    Set objScript = NOTHING
END FUNCTION
%>

答案 2 :(得分:0)

当您使用AJAX / Post时,正常的“文本”变为“不安全”字符,因此您必须使用encodeURI在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);

结果将是:

HelloWorld!

因此,为了对URL进行编码,您必须使用JavaScript方法

对其进行编码
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);

然后结果会像:

Hello World!

所以问题是如何“解码”编码的URI以便在ASP Classic服务器页面中使用它

编辑:

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