【问题标题】:Pass a variable from Ajax to VB Script Function in classic ASP将变量从 Ajax 传递到经典 ASP 中的 VB 脚本函数
【发布时间】:2017-07-18 22:08:48
【问题描述】:

我有一个经典的 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)
%>

【问题讨论】:

  • 你不能。 JavaScript 运行在客户端,VB 运行在服务器端。
  • 不能直接从客户端调用服务端代码,需要使用ajax
  • @dave 感谢您的及时回复。我很害怕,但我是 Web 开发初学者。您介意提供一些参考资料吗?
  • @Ilya Bursov 我无法在上述评论中标记多个人。如果我能推荐一些东西,将不胜感激。
  • 服务器端代码始终在客户端执行任何操作之前执行,因此您需要通过表单提交或 ajax 以某种方式将数据发送回服务器,如前所述.我会用谷歌搜索“jquery ajax”来开始熟悉它。

标签: ajax vbscript parameters asp-classic parameter-passing


【解决方案1】:

它可能无法正常工作,因为 alert 语句无效。您正在尝试警告不带引号的字符串!如果你想做你正在尝试的事情(在这种情况下,警报是无用的,因为它在 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,它就会出现。选择 CONSOLE 选项卡(在 Chrome 中),如果存在任何 javascript 错误,您将看到它们。

祝你好运!

【讨论】:

  • 非常感谢您的帮助。我修改了我的代码并发布了更多详细信息代码,因为我正在使用此逻辑自动完成文本框从数据库中检索数据。它不会在控制台下抛出任何错误,但似乎没有将变量 strUserInput 正确传递给 VB 脚本。该查询忽略 strUserInput 的选择条件。我可以对元素使用相同的 ASP 代码本身吗:URL?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-25
    • 2017-12-26
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多