Question

I've working on a large asp classic site and trying to introduce couple of new vbscript function that will be triggered from jQuery based on some event, after which jQuery will do a partial update of the page.

My question is: If all the code (ASP classic vbscript function + html and jquery) are all in the same file (there are many vbscript functions in this page), how can i make a ajax call to a function in this page, get it to be processed on server and respond back.

So for example:

    <html>
        <head>
            .....
                <%
                    Function  checkUsername( ....)
                        ....
                    end function

                    Function  checkCredentials( .... )
                      .....
                    end function
                %> 
            <script>
            $(document).ready(function(){
                $( "#username" ).blur(function() {
                    var username = $('#username_field").val();
                    checkUsername(username);
                });

                function checkUsername(usr) //the function that loads pages via AJAX            
                {
                    $.ajax({    
                        type: "GET",
                        url: "WHAT TO PUT HERE?????",
                        data: usr,
                        cache: false,   
                        success: function(msg){
                            ....
                        }
                    });
                }
            });
            </script>
        </head>
        <body>
            ....
            Username: <input type="text" name="username" id="username">
                            .....
        </body>
    </html>
Was it helpful?

Solution

You can't directly call a VBScript function from jQuery/JavaScript/AJAX per se, but you could just pass a URL variable that triggers your ASP code to run a specific VBScript function and then stop.

I'm not too familiar with the jQuery ajax function, but if you specified something like the following:

.ajax({
    type: 'GET',
    url: '<%= Request.ServerVariables("SCRIPT_NAME") %>?u=' + usr,
    ...
});

It should call your page again with the username passed as the u query string variable. Then, somewhere near the start of your ASP code, you'd want to do something like this:

<%
    Dim strUser
    strUser = Request.QueryString("u")

    If Len(strUser) > 0 Then

        ' Write an ajax response based on this user...
        Response.Write ...

        ' Stop processing...
        Response.End

    End If
%>

OTHER TIPS

Here is the basic idea, modify to meet your requirements.

Create a seperate page with just the code you want to execute like, getCustomers.asp:

<%
response.ContentType="text/HTML"
response.Charset="ISO-8859-1"

on error resume next
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open("C:/WebData/Northwind.mdb")
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT CompanyName,City,Country FROM Customers"

rs.Open sql, conn
if err <> 0 then
  response.write(err.description)
  set rs=nothing
  set conn=nothing
else
  response.write("<table border='1'>")
  response.write("<tr><th>Name</th><th>City</th><th>Country</th></tr>")
  do until rs.EOF
    response.write("<tr>")
    for each x in rs.Fields
      response.write("<td>" & x.value & "</td>")
    next
    response.write("</tr>")
    rs.MoveNext
  loop
  response.write("</table>")
  rs.close
  conn.close
end if
on error goto 0
%>

If you are only doing a get then your jquery call could do this:

$.get("getCustomers.asp", function(data){
  alert("Result: " + data);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top