2015-10-07 38 views
4

我試圖讓recapatcha v2在我的ASP MVC項目中工作。客戶端的計算機具有IE10/IE11,並在兼容性視圖中顯示所有Intranet頁面,這會導致recaptcha無法按預期顯示。ReCAPTCHA v2不能在IE兼容視圖

問題是它很少接受我的答案,即使它是正確的。它只是顯示一個新的圖像,但每隔一段時間我就會明白。任何人都會遇到這種情況

如果您在IE中爲google.com啓用compability視圖並訪問the demo site,則可以試用它。

回答

2

你看到的reCAPTCHA回落。看來你必須連續得到兩個正確的答案。然後它會爲您提供一個響應代碼,您可以將其複製並粘貼到<textarea>元素中。

所以你可能遇到的是,你沒有得到正確的兩個連續reCaptchas。

You can test the fallback recaptcha by adding the fallback=true parameter to the JavaScript resource

<script src="https://www.google.com/recaptcha/api.js?fallback=true" async defer></script> 

正如@Coulton recaptcha does not support IE compatibility mode.解釋

+0

要添加到此,不幸的是,即使您強制IE進入邊緣模式,reCaptcha仍會顯示後備版本。 – Paesano2000

1

驗證碼使用的JavaScript函數命名querySelectorAll和querySelector。兼容模式下的IE 11呈現爲IE 7.0,並且IE 7.0及以下似乎沒有函數querySelectorAll和querySelector,這就是爲什麼它失敗。

現在,我使用的.NET Web表單,所以你可能不得不適應它一點點的MVC,但在這裏它是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="reCaptcha.aspx.cs" Inherits="reCaptcha" %> 

<!DOCTYPE html> 
<script type="text/javascript"> 
    // this defines these functions if they don't exist. 
    if (!document.querySelectorAll) { 
     document.querySelectorAll = function (selectors) { 
      var style = document.createElement('style'), elements = [], element; 
      document.documentElement.firstChild.appendChild(style); 
      document._qsa = []; 

      style.styleSheet.cssText = selectors + 
       '{x-qsa:expression(document._qsa && document._qsa.push(this))}'; 
      window.scrollBy(0, 0); 
      style.parentNode.removeChild(style); 

      while (document._qsa.length) { 
       element = document._qsa.shift(); 
       element.style.removeAttribute('x-qsa'); 
       elements.push(element); 
      } 
      document._qsa = null; 
      return elements; 
     }; 
    } 

    if (!document.querySelector) { 
     document.querySelector = function (selectors) { 
      var elements = document.querySelectorAll(selectors); 
      return (elements.length) ? elements[0] : null; 
     }; 
    } 
</script> 
<script src='https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.js' type="text/javascript"></script> 
<script src='https://www.google.com/recaptcha/api.js' type="text/javascript"></script> 
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/1.3.2/jquery.min.js' type="text/javascript"></script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>reCaptcha Test</title> 

</head> 
<body> 
    <h1>reCaptcha Test</h1> 
    <form id="frmResult" runat="server"> 
    <div class="g-recaptcha" data-sitekey=""></div> 
     <script type="text/javascript"> 
     function IeVersion() { 
        //Set defaults 
        var value = { 
         IsIE: false, 
         TrueVersion: 0, 
         ActingVersion: 0, 
         CompatibilityMode: false 
        }; 

        //Try to find the Trident version number 
        var trident = navigator.userAgent.match(/Trident\/(\d+)/); 
        if (trident) { 
         value.IsIE = true; 
         //Convert from the Trident version number to the IE version number 
         value.TrueVersion = parseInt(trident[1], 10) + 4; 
        } 

        //Try to find the MSIE number 
        var msie = navigator.userAgent.match(/MSIE (\d+)/); 
        if (msie) { 
         value.IsIE = true; 
         //Find the IE version number from the user agent string 
         value.ActingVersion = parseInt(msie[1]); 
        } else { 
         //Must be IE 11 in "edge" mode 
         value.ActingVersion = value.TrueVersion; 
        } 

        //If we have both a Trident and MSIE version number, see if they're different 
        if (value.IsIE && value.TrueVersion > 0 && value.ActingVersion > 0) { 
         //In compatibility mode if the trident number doesn't match up with the MSIE number 
         value.CompatibilityMode = value.TrueVersion != value.ActingVersion; 
        } 
        return value; 
       } 
     var ie = IeVersion(); 
     var reCaptchaApi = ""; 

     $(document).ready(function() { 
      $('.g-recaptcha').each(function (index, obj) { 
      grecaptcha.render(obj, { 'sitekey': reCaptchaApi }); 
     }); 
     if (ie.CompatibilityMode) { 
      // loading it twice makes it load in compatibility mode. 
      $('.g-recaptcha').each(function (index, obj) { 
       grecaptcha.render(obj, { 'sitekey': reCaptchaApi }); 
      }); 
     } 
     }); 
    </script> 
</form> 

</body> 
</html> 

這讓reCAPTCHA的V-2在兼容模式下加載。