如何将 VBScript 变量传递给服务器端 JScript?

How do I pass VBScript variables to server side JScript?

我有一个包含的 JScript(服务器端),我需要将一些变量从 VBScript 传递给它,但是我使用 ASP Classic 中的传统方法所做的努力对我不起作用,我什至尝试过发送带有 javascript include..

的查询字符串

我的 VBScript 页面:

<%
    Dim Tomorrow, TomorrowDay, TomorrowMonth, TomorrowYear, NewTomorrow, Today, TodayMonth, TodayYear, JSONConvertAPIStatsURL
    Tomorrow = DateAdd("d",1,now())

    TomorrowDay = Right("0" & Day(Tomorrow), 2)
    TomorrowMonth = Right("0" & Month(Tomorrow), 2)
    TomorrowYear = year(Tomorrow)
    NewTomorrow = TomorrowYear & "-" & TomorrowMonth & "-" & TomorrowDay
    Today = now()
    TodayMonth = Right("0" & Month(Today), 2)
    TodayYear = year(Today)
%>

<script language="JavaScript" runat="Server" src="retrieve_convertapi_stats.asp"></script>

<%


  Dim UpdateConvertAPIJSONData
    Set UpdateConvertAPIJSONData = Server.CreateObject("ADODB.Connection")
    UpdateConvertAPIJSONData.ConnectionString="Provider=SQLOLEDB; DATA SOURCE=RPASQL01;UID=<USERNAME>;PWD=<PASSWORD>;DATABASE=<DATABASE>"
    UpdateConvertAPIJSONData.Open

    Dim UpdateConvertAPIJSONDataSQL, UpdateConvertAPIJSONDataObj

    UpdateConvertAPIJSONDataSQL = "UPDATE EFP_JSON SET CONVERTAPI_STATS = '" & objSrvHTTP.responseText & "' WHERE ID = 1;"
    Response.Write UpdateConvertAPIJSONDataSQL

    Set UpdateConvertAPIJSONDataObj = UpdateConvertAPIJSONData.Execute(UpdateConvertAPIJSONDataSQL)

    UpdateConvertAPIJSONData.Close
    Set UpdateConvertAPIJSONData = Nothing

    Response.Write now()

%> 

我的 JScript (retrieve_convertapi_stats.asp)

Response.CacheControl = "no-cache"
Response.Expires = -1
Response.CodePage = 65001
Response.CharSet = "UTF-8"

var objSrvHTTP;
objSrvHTTP = Server.CreateObject ("Msxml2.ServerXMLHTTP.6.0");
objSrvHTTP.open ("GET","https://api.theurl.com/user/statistic?secret=<MY_API_KEY>&startDate=<%=TodayYear%>-<%=TodayMonth%>-01&endDate=<%=NewTomorrow%>", false);
objSrvHTTP.send ();
Response.ContentType = "application/json";

我怎样才能做到这一点?

您不能将变量传递给 JScript,只有在 JScript 中创建的变量才能在 VBscript 中访问(无论出于何种原因都是如此)。

我建议您在 VBScript 中创建整个流程,因为 JScript 中的功能可以在 VBScript 中完成,您不会有任何问题。

<%
    Dim Tomorrow, TomorrowDay, TomorrowMonth, TomorrowYear, NewTomorrow, Today, TodayMonth, TodayYear, JSONConvertAPIStatsURL
    Tomorrow = DateAdd("d",1,now())

    TomorrowDay = Right("0" & Day(Tomorrow), 2)
    TomorrowMonth = Right("0" & Month(Tomorrow), 2)
    TomorrowYear = year(Tomorrow)
    NewTomorrow = TomorrowYear & "-" & TomorrowMonth & "-" & TomorrowDay
    Today = now()
    TodayMonth = Right("0" & Month(Today), 2)
    TodayYear = year(Today)

    Dim xml, url
    Set xml = CreateObject("Msxml2.ServerXMLHTTP.6.0")
    url = "https://api.theurl.com/user/statistic?secret=<MY_API_KEY>&startDate=" & TodayYear & "-" & TodayMonth & "-01&endDate=" & NewTomorrow
    xml.open "GET", url, false
    xml.setRequestHeader "Content-Type", "application/json"

    xml.Send
    status = trim(xml.status)         ' server status
    returnresponse = xml.responseText ' entire body
%>

您可以通过动态写入 JavaScript 将变量从 VB 传递给 JavaScript,以便它可以包含新变量。

VB (ASP) 代码在服务器端执行,所以它会将修改后的 JavaScript 发送给用户,稍后在浏览器中执行,没问题。