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