0

为什么 JavaScript 警报不会在 ASP 经典 Select Case 分支中弹出?当我放入 Response.End 语句并运行结帐过程时,JavaScript 警报会做他们的事情。但是当我将它们取出时,它们会立即跟随重定向...警报不会弹出。知道为什么吗?

If strResponse <> "000" Then
Select Case strResponse
    Case "701"
        %>
        <script type="text/javascript">
            alert("The customer is under 18 years of age based upon the date of birth.");
        </script>
        <%
        'Response.End
        Response.Redirect strRedirectCheckout
    Case "702"
        %>
        <script type="text/javascript">
            alert("The billing address is outside the United States.");
        </script>
        <%
        'Response.End
        Response.Redirect strRedirectCheckout
    Case "707"
        %>
        <script type="text/javascript">
            alert("The account holder does not have sufficient credit available for the transaction amount.");
        </script>
        <%
        'Response.End
        Response.Redirect strRedirectCheckout
    Case Else
        %>
        <script type="text/javascript">
            alert("Unable to obtain an authorization\nClick OK to be redirected back to checkout and please choose another form of payment");
        </script>
        <%
        'Response.End
        Response.Redirect strRedirectCheckout
End Select

Else ' if strResponse does == '000'; SUCCESS!!!
4

3 回答 3

4

当您使用 Response.Redirect 时,您正在影响请求的响应代码。当浏览器加载一段内容时,它会得到一个返回码,例如 200 或 401,指示请求的状态。Response.Redirect 发送一个返回码,导致浏览器不呈现它收到的任何内容,而是重定向响应。没有它,浏览器会收到 200 响应并呈现它收到的内容。

HTTP 响应代码详见http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

尤其看

10.3 重定向 3xx

此类状态码表明用户代理需要采取进一步的行动才能满足请求。

本质上,您不能指示浏览器(通过 HTTP 状态代码)同时呈现页面并将其重定向到其他地方。这些是互斥的,重定向胜过渲染。

于 2012-04-27T15:53:12.427 回答
4

因为 Response.Redirect 向浏览器发送消息以开始导航到指示的页面。如果您希望他们首先看到弹出窗口,则在警报之后只需停留在 JavaScript 中并使用 document.location.href=.....

于 2012-04-27T15:53:25.843 回答
1

我会开始想知道为什么您首先将服务器验证客户端验证混为一谈...

客户端验证是在对其自身的页面执行任何操作之前进行的验证,换句话说,在表单的实际 POST 之前。

服务器验证应该在服务器端执行相同的操作,并且应该只发出一条普通消息,例如,可以填充一个<div>

从你的例子我会这样做:


1 -添加客户端验证

假设您正在使用 jQuery(以方便代码,因为这个想法只是向您展示要采取的路径......),并且您有一个 POST 按钮,编码为

<input type="submit" id="btnSave" value="Save" />

将此添加到页面末尾的</body>标记之前:

<script type="text/javascript">
$(function() {

  $("form").submit(function() {
    // before the form submit, do this:

    var msg = '';

    if(parseInt($(".input-age-year").val()) < 1994)
        msg += '\nThe customer is under 18 years of age based upon the date of birth.';
    if($(".input-country").val() != 'USA')
        msg += '\nThe billing address is outside the United States.';
    if(parseInt($(".hidden-credit").val()) < parseInt($(".input-amount").val()))
        msg += '\nThe account holder does not have sufficient credit available for the transaction amount.';
    // etc...

    if(msg === '') 
       // message is empty, all is well, let's submit the form
       return true;
    else {
       // there is an error message to show
       alert(msg); // I would show an error div with the message instead though...
    }

  });

});

</script>

顺便说一句,你应该看到jQuery Validation Plugin,它好多了!


2 -做你的服务器验证

这始终很重要,因为您可以在不启用 javascript 的情况下提交,它有助于缩小潜在的“黑客”范围

做同样的事情,但重定向到同一页面,向 URL 添加查询字符串或使用Session对象来保存错误(记住在页面中显示消息后将其删除)

例如:

Dim msg As String = ""

If cInt(Request("frm-age-year")) < 1994 Then
    msg = "<br/>The customer is under 18 years of age based upon the date of birth."
End If

' ...

If msg = "" Then
  ' Continue processing ...
Else
  Response.Redirect Request.ServerVariables("URL") & "?error=" & msg
End if

.asp你有一个地方像

<% If Request("error") <> Null Then %>
    <div class="error"><%= Request("error") %></div>
<% EndIf %>
于 2012-04-27T16:14:17.197 回答