-1

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


%>
Stig Kølbæk
  • 432
  • 2
  • 18
  • jQuery will be expecting a JSON response to parse, try `Response.Write "{ ""result"": 0 }"` instead. – user692942 May 03 '21 at 11:36
  • @user692942 .. I have now, as you suggested changed to `Response.Write "{ ""result"": 0 }"` and `Response.Write "{ ""result"": 1 }"`, and if I call the ASP page directly I get the JSON result, but the jQuery still not gives me an alert. – Stig Kølbæk May 03 '21 at 11:53
  • Because you are returning a JSON object containing a `result` property, so you have to reflect that in your check, something like`if (response.result == 1)` come on you should be figuring that out. A quick look in dev tools in the browser would have revealed that much. Don’t understand when people are reluctant to do basic debugging and instead just expect the answer. – user692942 May 03 '21 at 12:01
  • Yes @user692942, that goes without saying, so offcause I changed the IF clause of the jQuery to reflect the response from the ASP page .. but that still not change the fact that I do not get the alert. :-) – Stig Kølbæk May 03 '21 at 12:08
  • @user692942 and regarding debugging, I have developer tools open all the time for this, but when I do not get any errors, debugging is really hard. – Stig Kølbæk May 03 '21 at 12:16
  • Glad to hear, can you [edit] the question to modify the code to reflect what you’ve changed? You also place a breakpoint on the `if` statement that way you should be able to see what path the code takes. – user692942 May 03 '21 at 12:17
  • @user692942 .. Have just added the new code below **Updated Code below this:** .. The Console.Log does not return anything eigther .. It is like the code inside `function RequestValueFromTable() ` does not run – Stig Kølbæk May 03 '21 at 12:29
  • 1
    You need to invoke the function `RequestValueFromTable()`, now you have just defined it. After the line `console.log('setInterval was entered');`, add `RequestValueFromTable();` – Flakes May 03 '21 at 12:53
  • @user692942 .. I forgot to mention that only `console.log('setInterval was entered');` is howing in the console, that is every 5 sek, but nothing else, so the issue must lay within `function RequestValueFromTable()` – Stig Kølbæk May 03 '21 at 12:56
  • @Flakes ..Okay, brilliant ..Thanks .. that I forgot, .. so now I get "No Value was returned" from console so the function was entered .. next issue is that given the value in DB is at the moment 0 it should have written "Value 0 was returned" in the console, so I guess my IF statement has a flaw – Stig Kølbæk May 03 '21 at 13:01
  • 1
    `console.log(response.result)` and see what it shows. – Flakes May 03 '21 at 13:17
  • @Flakes .. OK, I get `undefined` .. but is this from the jQuery or the ASP side .. Maybe the JSON is wrong – Stig Kølbæk May 03 '21 at 13:27
  • 1
    `var thejson = JSON.parse(response)` , And Then, `if (thejson.result == 1)....`. Should do it. – Flakes May 03 '21 at 13:27
  • YES, Thank you @Flakes I now get the alert and the correct value in the console so running it down the issue was that I did not invoke `RequestValueFromTable()` and that `var thejson = JSON.parse(response)` was not defined within `function verifyDb(response)` .. Please post an answer so that I can give you the credit :-) – Stig Kølbæk May 03 '21 at 13:36
  • @Flakes the `$.get()` method [supports `json` out if the box](https://stackoverflow.com/a/17203248/692942) no need for the extra parse. – user692942 May 03 '21 at 15:34
  • Does this answer your question? [Parsing JSON data from classic asp server?](https://stackoverflow.com/questions/9816304/parsing-json-data-from-classic-asp-server) – user692942 May 03 '21 at 15:37
  • @user692942 Yes, Okay, could have done that. OP was not using json in the first place, was returning 0 and 1 and it would have worked if only that function call was added. But then json came into the picture... :Can also use $.getJson() instead. So many ways to do it. – Flakes May 03 '21 at 16:21

1 Answers1

-1

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

%>
user692942
  • 16,398
  • 7
  • 76
  • 175
Stig Kølbæk
  • 432
  • 2
  • 18
  • 1
    You wouldn’t have needed the JSON parse if you had [specified `json` as the return type](https://stackoverflow.com/a/17203248/692942). – user692942 May 03 '21 at 15:31