0

ASP の従来の Select Case ブランチで JavaScript アラートがポップアップ表示されないのはなぜですか? 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 に答える