1

I have an included JScript (Server side) that I need to pass some variables to from VBScript, but my effort using the traditional methods in ASP Classic has not worked for me, I have even tried to send a querystring with the javascript include..

My VBScript Page:

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

%> 

My 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";

How can I achive this?

Stig Kølbæk
  • 432
  • 2
  • 18
  • 3
    You could enclose the whole jscript/javascript code in a function with the required variables as the parameters (like `function yourfunction(TodayYear,TodayMonth,NewTomorrow))` and call the function from the vbscript part passing the values as `yourfunction TodayYear,TodayMonth,NewTomorrow`. Within the function you need to concatenate the values as `"...&startDate=" + TodayYear + "-" + TodayMonth + "-01&endDate=" + NewTomorrow + ""`. Should work. – Flakes Jun 15 '21 at 10:58
  • Does this answer your question? [HTTP GET in VBS](https://stackoverflow.com/questions/204759/http-get-in-vbs) – user692942 Jun 15 '21 at 18:21

2 Answers2

2

You can't pass variables to the JScript, only variables created in the JScript can be accessed in the VBscript (for whatever reason this is how it is).

I recommend you create the entire process in VBScript as the functions in JScript can be done in VBScript and you won't have any problems.

<%
    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
%>
silver
  • 650
  • 4
  • 15
  • 2
    WOW @silver .., this was an even better answer than I could have hoped for in my dreams!! .. I had looked for a way to do this in VBScript only, but did not find it .. THANK YOU for this solution, I really appriciate it :-) – Stig Kølbæk Jun 15 '21 at 13:22
  • @StigKølbæk you looked, are you serious? `Msxml2.ServerXMLHTTP`is Classic ASP bread and butter. Guess you missed [HTTP GET in VBS](https://stackoverflow.com/a/208913)? – user692942 Jun 15 '21 at 18:19
-1

You can pass variables to JavaScript from VB by writing in the JavaScript dynamically so that it can include the new variables.

VB (ASP) code is ececuted server side, so it will send the modified JavaScript to the user where it is later actioned in the browser, no problem.

WilliamK
  • 821
  • 1
  • 13
  • 32