2012-04-27 25 views
0

爲什麼在ASP經典的Select Case分支中不會彈出JavaScript警報? 當我放入Response.End語句並運行結帳過程時,JavaScript警報會執行它們的操作..但是當我將它們取出並且緊接着是重定向時...警報不會彈出。任何想法爲什麼?爲什麼不會在ASP經典的Select Case分支中彈出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

因爲Response.Redirect向瀏覽器發送消息以開始導航到指定的頁面。如果你想讓他們先看到彈出窗口,在alert後留在JavaScript中並使用document.location.href = .....

+1

這工作像一個魅力。謝謝。 – JoJo 2012-04-27 16:01:32

4

當您使用Response.Redirect時,您正在影響請求的響應代碼。當瀏覽器加載一段內容時,它會得到一個返回碼,例如200或401,表示請求的狀態。 Response.Redirect發送一個返回碼,導致瀏覽器不呈現任何接收到的內容,而是重定向響應。沒有它,瀏覽器會收到200響應並呈現收到的內容。

的HTTP響應代碼在http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

詳述參見尤其

10.3重定向3XX

這個類的狀態代碼表示進一步的動作需要是 由用戶代理所採取爲了履行要求。

本質上,您不能指示瀏覽器(通過HTTP狀態碼)同時渲染頁面並將其重定向到其他位置。這些是互斥的,重定向勝過渲染。

+0

的地方謝謝你的解釋!並閱讀材料。 – JoJo 2012-04-27 16:02:34

1

我會開始疑惑你爲什麼要把服務器驗證混淆了客戶端驗證擺在首位...

客戶端驗證是發生之前採取任何行動做是爲了在網頁它的自我,換句話說,表單的實際POST前的驗證。

服務器驗證應執行相同的,但在服務器端,只能發出一個正常的消息,例如,可以fillup一個<div>

從你的榜樣,我會做這樣的:


1 - 添加客戶端驗證

讓我們假設你使用JQ uery(爲了方便代碼,因爲這個想法只是爲了向你展示採取的路徑...),和你有一個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對象來保存錯誤(請記住在頁面中顯示消息後將其刪除)

for例如:

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