javascriptvbscriptasp-classicscheduled-tasks

JavaScript won't execute when calling webpage via VBScript using XMLHttpRequest


I have a process where I have created a polling function that executes an XMLHttpRequest via VBScript.

The request gets an ASP Classic page which checks an MSSQL table for newly inserted records and runs some APIs to add users to Azure AD.

I use jQuery for polling, then I use Windows Task Scheduler to run the VBScript every 5 minutes. Here is the code:

VBScript:

set xmlhttp = createobject("microsoft.xmlhttp")
xmlhttp.open "GET", "https://myserver.com/polling.asp?route=create-ad-account", false
xmlhttp.send
set xmlhttp = nothing

ASP classic code in the GET request:

...

action = request("action")
route = request("route")

openDB()
select case action
    case "create-ad-account" : createADAccount()
    case else : default()
end select
closeDb()
...

function createADAccount()
''Checks table for new records, add to AD accordingly.
...

end function

function default()
%>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>

        //Get route
        var route = getUrlVars()["route"];

        //Autorun
        if(route!=''){
            doPoll('?action=' + route);
        }

        function doPoll(url) {
            var result;

            $.post(url, function(data) {
                result = data;
            })

            .done(function (){
                console.log(result);  //Show results here
                if(result!=''){
                    setTimeout(doPoll(url),1000);
                } else {
                   console.log('Task competed');
                }
            })

            .fail(function (jqXHR, textStatus, errorThrown){
                console.log('Error: ' + textStatus);
            })
        }

        function getUrlVars(){
            var vars = [], hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for(var i = 0; i < hashes.length; i++)
            {
                hash = hashes[i].split('=');
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
            return vars;
        }
    </script>
<%
end function

The page in the get requests checks for newly added records to a users table which then checks to see if the user exists in AD, if not it adds them via an API call.

It all works fine when loading the page from a browser, when running it from task scheduler it reports back 0x0 meaning successfully executed but nothing is done.

I am assuming the JavaScript in the default() function is not executed when running from the VBScript as I tried running the VBScript directly and nothing is done (i.e. the createADAccount function does not run).


Solution

  • This will never work as you expect because the XMLHTTP request doesn't know how to process client-side code, it is not an Internet Browser. It has no concept of a JavaScript Engine, it just sends and receives HTTP in its raw form.

    Tasks such as processing client-side JavaScript and rendering CSS / DOM are built into Internet Browser applications.

    As an XMLHTTP response deals with the raw HTTP response you would need to parse the script and execute it yourself.


    Useful Links