ASP经典内联代码+ jQuery:如何做ajax调用

时间:2022-08-26 20:24:00

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.

我在一个大型的asp经典站点上工作,并尝试引入一些新的vbscript函数,这些函数将基于一些事件从jQuery触发,之后jQuery将对页面进行部分更新。

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.

我的问题是:如果所有的代码(ASP经典vbscript函数+ html和jquery)都是在同一个文件中(在这个页面有很多vbscript功能),我怎么能让一个ajax调用一个函数在这个页面,让它在服务器处理和响应。

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>

2 个解决方案

#1


2  

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.

您不能直接从jQuery/JavaScript/AJAX调用VBScript函数,但您可以传递一个URL变量,该变量触发您的ASP代码运行特定的VBScript函数,然后停止。

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

我不太熟悉jQuery ajax函数,但是如果您指定如下内容:

.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:

它应该再次调用您的页面,使用作为u查询字符串变量传递的用户名。然后,在接近ASP代码开始的地方,您可能想要做如下的事情:

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

#2


1  

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:

创建一个单独的页面,其中只包含您想要执行的代码,如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,那么你的jquery调用可以这样做:

$.get("getCustomers.asp", function(data){
  alert("Result: " + data);
});

#1


2  

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.

您不能直接从jQuery/JavaScript/AJAX调用VBScript函数,但您可以传递一个URL变量,该变量触发您的ASP代码运行特定的VBScript函数,然后停止。

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

我不太熟悉jQuery ajax函数,但是如果您指定如下内容:

.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:

它应该再次调用您的页面,使用作为u查询字符串变量传递的用户名。然后,在接近ASP代码开始的地方,您可能想要做如下的事情:

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

#2


1  

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:

创建一个单独的页面,其中只包含您想要执行的代码,如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,那么你的jquery调用可以这样做:

$.get("getCustomers.asp", function(data){
  alert("Result: " + data);
});