将变量从 Ajax 传递到 VB 经典 ASP 中的脚本函数

Pass a variable from Ajax to VB Script Function in classic ASP

我有一个经典的 ASP 代码,我想将变量 terms 从 Ajax 传递到 VB 脚本函数。我尝试了下面的代码,但它不起作用。

这是我第一次使用 ajax 编写代码。所以我知道这是非常基本的。但是我找不到哪里错了。有谁能帮帮我吗?

<script type="text/Javascript">        
    $(document).ready( function(){  
var availableCode = new Array();            

function customFilter(terms) {
                $.ajax({
                 type: "POST",
                 url: "Test.asp",   // This asp file name itself
                 data: {"strUserInput": '"' + $("#terms").val() + '"'  },
                 cache: false,
                 success: function() { 
                        alert ("returned from server side");
                 }
             });

            <%
            Dim idxJs
            for idxJs = 0 to 19 
            %>
                availableCode[<%=idxJs %>] = unescape('<%= Escape(codeList(idxJs)) %>');

            <% next %>

                return availableCode;
            };

            $( "#frmBillCode" ).autocomplete({
              multiple: true,
              mustMatch: false,
              minLength: 4,
              delay: 100,
              search: function (event,ui) {
                window.pageIndex = 0;
                },
              source: function (request, response) {
                response(customFilter(request.term));
              }
            });
        } );
</script>   

<%
    Dim strUserInput
    strUserInput = Request.Form("strUserInput")
    Document.write(strUserInput)
%>

它可能不工作,因为警告语句无效。您正在尝试提醒字符串 w/out 引号!如果你想做你正在尝试的事情(在这种情况下,警报是无用的,因为它在 ajax 调用之后并且无法访问存储在 strUserInput 中的新值),它需要用引号引起来:

alert('<%=strUserInput%>');

但同样,您不需要它,我只是想解释您可能失败的原因。

让我们试试这个:

<%
    Dim strUserInput
    strUserInput = Request.Form("strUserInput")
    if strUserInput <> "" then      '-- we know it's an ajax call
       Response.Write(strUserInput)
       Response.End       '-- when doing ajax calls, it's good to add this line so that nothing after this line is sent back to the client
    end if
%>

<script type="text/Javascript">        
    $(document).ready( function(){  
        customFilter();     // you need to call your function on page load for it to do something
        function customFilter() {
            var terms = 'abc';      // what is this line for?
            $.ajax({
                 type: "POST",
                 url: "Test.asp",   << This asp file name
                 data: {strUserInput: '"' + $("#terms").val() + '"'  },   // removed quotes from strUserInput
                 cache: false,
                 success: function( result ) {     // result is just a variable, it can be named anything 
                        alert ( result );
                 }
             });
       }
    });
</script>   

如果仍然没有任何反应,请了解您的开发人员控制台,在大多数浏览器中,您可以按 F12,它就会出现。 select CONSOLE 选项卡(在 Chrome 中),您将看到任何 javascript 错误(如果存在)。

祝你好运!

由于这个逻辑在我们的系统中不起作用,我改为使用延迟加载,现在它正在处理我们的业务需求。再次感谢大家的指教