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?

2
  • 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. Commented Jun 15, 2021 at 10:58
  • Does this answer your question? HTTP GET in VBS Commented Jun 15, 2021 at 18:21

2 Answers 2

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
%>
Sign up to request clarification or add additional context in comments.

2 Comments

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 :-)
@StigKølbæk you looked, are you serious? Msxml2.ServerXMLHTTPis Classic ASP bread and butter. Guess you missed HTTP GET in VBS?
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.