Search code examples
jqueryasp-classic

How to get response from ASP Classic on jQuery request


I have build the below jQuery to get a response from an ASP Classic page running an Database query (MS SQL 2019) when clicked on a button.

My goal is to check if a certain field in the DB has changed, and if not ask again after 5 seconds, if it change and the value is 1 we have a success and should return some extra values to the page running the jQuery, if the value is 0 there is an error and I need to return that info, but until I get value in the field in the table it should pause for 5 seconds and run the request again until I get a response.

I have tried both with native ASP Classic Response, and also with JSON, but at this point it seems that if I place an alert after setInterval(function(){ it returns, but not after function RequestValueFromTable() so I gues that the last function has some errors, but I cannot seem to figure out why since I get no errors in the console.

So my questions is:

  1. What am I doing wrong in the function RequestValueFromTable() and below?
  2. What is the correct way to send the responses back to jQuery from ASP DB Query?

My jQuery:

function RunTableEntryValidation()
{

   setInterval(function(){ 

   
    function RequestValueFromTable()
    {
    var url="check_usercreation_progress.asp?id=<%=objGetEmployeeName("ID")%>";
    $.get(url,{},verifyDb);
    }

    function verifyDb(response)
    {
        
        if (response==1)
        {
        clearInterval();
        alert("I have the value");
        //The value exists here I will run function
        }

        else if (response==0)
        {
        clearInterval();
        alert("I do not have the value");
        }
        
        else
        {
        };            

    };

}, 5000);

};

check_usercreation_progress.asp:

<%@ Language=VBScript %>
<%
option explicit
Response.Expires = -1
Server.ScriptTimeout = 600
Session.CodePage  = 65001
Response.CharSet = "UTF-8"

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

Dim CheckUserCreationStatusSQL, CheckUserCreationStatusObj

CheckUserCreationStatusSQL = "SELECT * from EFP_EmploymentUser WHERE ID = " & Request.QueryString("id") & ";"

Set CheckUserCreationStatusObj = CheckUserCreationStatus.Execute(CheckUserCreationStatusSQL)


IF CheckUserCreationStatusObj("HasChachedAD") = 1 THEN 

Response.Write "1"

ELSEIF CheckUserCreationStatusObj("HasChachedAD") = 0 THEN 

Response.Write "0"

ELSE

END IF


CheckUserCreationStatus.Close
Set CheckUserCreationStatus = Nothing


%>

Updated Code below this:

function RunTableEntryValidation()
{

setInterval(function(){ 

    console.log('setInterval was entered');


    function RequestValueFromTable()
    {
    var url="check_usercreation_progress.asp?id=<%=objGetEmployeeName("ID")%>";
    $.get(url,{},verifyDb);
    console.log('RequestValueFromTable was entered');
    }

    function verifyDb(response)
    {
        console.log('verifyDb was entered');
        
        if (response.result == 1)
        {
        console.log('Value 1 was returned');
        alert("I have the value");
        clearInterval();
        }

        else if (response.result == 0)
        {
        console.log('Value 0 was returned');
        alert("I do not have the value");
        clearInterval();
        }
        
        else
        console.log('No Value was returned');
        {
        };            

    };

}, 5000);

};
<%@ Language=VBScript %>
<%
option explicit
Response.Expires = -1
Server.ScriptTimeout = 600
Session.CodePage  = 65001
Response.CharSet = "UTF-8"

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

Dim CheckUserCreationStatusSQL, CheckUserCreationStatusObj

CheckUserCreationStatusSQL = "SELECT * from EFP_EmploymentUser WHERE ID = " & Request.QueryString("ID") & ";"

Set CheckUserCreationStatusObj = CheckUserCreationStatus.Execute(CheckUserCreationStatusSQL)


IF CheckUserCreationStatusObj("ADCreationStatus") = 1 THEN 

Response.Write "{ ""result"": 1 }"

ELSEIF CheckUserCreationStatusObj("ADCreationStatus") = 0 THEN 

Response.Write "{ ""result"": 0 }"

ELSE

Response.Write "{ ""result"": ""empty"" }"

END IF


CheckUserCreationStatus.Close
Set CheckUserCreationStatus = Nothing


%>


Solution

  • The solution was drilled down the following steps:

    1. Response.Write in ASP page should be a JSON formated response: Response.Write "{ ""result"": 1 }"
    2. RequestValueFromTable() was not invoked after setInterval(function(){
    3. var thejson = JSON.parse(response) needed to be defined after function verifyDb(response) and the IF sentense changed to if (thejson.result == 1)

    The working code:

    function RunTableEntryValidation()
    {
        
        setInterval(function(){ 
    
            console.log('setInterval was entered');
    
            RequestValueFromTable();
    
            function RequestValueFromTable()
            {
            var url="check_usercreation_progress.asp?id=<%=objGetEmployeeName("ID")%>";
            $.get(url,{},verifyDb);
            console.log('RequestValueFromTable was entered');
            }
    
            function verifyDb(response)
            {
    
                var thejson = JSON.parse(response)
    
                console.log('verifyDb was entered');
                console.log(response.result)
                
                if (thejson.result == 1)
                {
                clearInterval();
                console.log('Value 1 was returned');
                alert("I have the value");
                }
    
                else if (thejson.result == 0)
                {
                clearInterval();
                console.log('Value 0 was returned');
                alert("I do not have the value");
                }
                
                else
                console.log('No Value was returned');
                {
                };            
    
            };
    
        }, 5000);
    
    };
    <%@ Language=VBScript %>
    <%
    option explicit
    Response.Expires = -1
    Server.ScriptTimeout = 600
    Session.CodePage  = 65001
    Response.CharSet = "UTF-8"
    
    Dim CheckUserCreationStatus
    Set CheckUserCreationStatus = Server.CreateObject("ADODB.Connection")
    CheckUserCreationStatus.ConnectionString="Provider=SQLOLEDB; DATA SOURCE=<SERVER>;UID=SA;PWD=<PASSWORD>;DATABASE=<DATABASE>"
    CheckUserCreationStatus.Open
    
    Dim CheckUserCreationStatusSQL, CheckUserCreationStatusObj
    
    CheckUserCreationStatusSQL = "SELECT * from EFP_EmploymentUser WHERE ID = " & Request.QueryString("ID") & ";"
    
    Set CheckUserCreationStatusObj = CheckUserCreationStatus.Execute(CheckUserCreationStatusSQL)
    
    
    IF CheckUserCreationStatusObj("ADCreationStatus") = 1 THEN 
    
    Response.Write "{ ""result"": 1 }"
    
    ELSEIF CheckUserCreationStatusObj("ADCreationStatus") = 0 THEN 
    
    Response.Write "{ ""result"": 0 }"
    
    ELSE
    
    Response.Write "{ ""result"": ""empty"" }"
    
    END IF
    
    CheckUserCreationStatus.Close
    Set CheckUserCreationStatus = Nothing
    
    %>