致力于在 CMS 集成网站中实施 recaptcha。首先是我的 HTMl 正文标记,它调用 javascript 方法以在加载正文时创建一个 recaptcha。
<body onload="return reloadRecaptcha();">
Recaptcha 使用以下代码出现在表单下方,这是我生成 recaptcha 的 js 方法。
function reloadRecaptcha() {
console.log("@ reloadRecaptcha ");
var publicKey = "";
var strServername = location.hostname;
if (strServername.indexOf("01") >= 0 || strServername.indexOf("02") >= 0 ){
publicKey = "HARDCODED_INTERNAL_KEY1";
} else {
publicKey = "HARDCODED_PUBLIC_KEY2";
}
var div = "recap";
if (!$("#message").is(":empty")) {
console.log("@ if Recaptcha.create ");
Recaptcha.create(publicKey,div,{theme: "red", callback: Recaptcha.focus_response_field});
} else {
console.log("@ else Recaptcha.create ");
Recaptcha.create(publicKey,div,{theme: "red"});
}
console.log("@ Exit reloadRecaptcha ");
return false;
}
当我将包含 repatcha 答案的表单提交给验证答案并在出错时转发回相同表单的 servlet 时,我在浏览器的控制台中收到以下错误。
- 在 CHROME 中,错误消息“Uncaught TypeError: Cannot set property 'innerHTML' of null”@ line recaptcha_ajax.js:112。
- 在 Firefox 中,错误消息“TypeError: Recaptcha.widget is null”@ line recaptcha_ajax.js:112。
我不知道为什么当我在错误时转发到表单 jsp 时,我才收到此错误,而不是在表单的初始加载期间。
我的服务器端代码(一个servlet)的代码片段:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("***** In the Captcha Validatation servlet**** ");
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
String remoteAddr = request.getRemoteAddr();
String strServerUrl = request.getServerName().toLowerCase();
String strPrivateKey = "";
if ((strServerUrl.indexOf("01") >= 0) || (strServerUrl.indexOf("02") >= 0))
strPrivateKey = getPropertyString("recaptcha.internal.privatekey");
else {
strPrivateKey = getPropertyString("recaptcha.external.privatekey");
}
reCaptcha.setPrivateKey(strPrivateKey);
String challenge = request.getParameter("recaptcha_challenge_field");
String uresponse = request.getParameter("recaptcha_response_field");
ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
HttpSession session = request.getSession(false);
if (session != null) {
String sessionId = session.getId();
Map paramMap = new HashMap();
for (Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {
String key = (String)e.nextElement();
String value = request.getParameter(key);
paramMap.put(key, value);
}
session.setAttribute("paramMap", paramMap);
if (reCaptchaResponse.isValid())
{
StringBuilder successUrl = new StringBuilder();
successUrl.append(getPropertyString("recaptcha.validationsuccess.url"));
successUrl.append("?evtvar=").append(request.getParameter("evtvar"));
successUrl.append("&vgnextoid=").append(request.getParameter("vgnextoid"));
RequestDispatcher rd = request.getRequestDispatcher(successUrl.toString());
rd.forward(request, response);
}
else
{
System.out.println("***** reCaptchaResponse INVALID *** ");
session.setAttribute("error1", getPropertyString("recaptcha.validationfailure.message"));
System.out.println("SERVLET PATH --> " + request.getServletPath());
String url_form = "/vgn-ext-templating/v/index.jsp?vgnextoid=40289c7c6099f210VgnVCM100000268315acRCRD&vgnextfmt=default";
System.out.println("Forwarding to the url after captcha validation --> " + url_form);
RequestDispatcher rd = request.getRequestDispatcher(url_form);
rd.forward(request, response);
}
}}
我从错误中看到的是,当表单重新加载以显示验证错误消息时,它找不到 div#recap
<div id="recap" style="height:130px;"></div>
它始终作为我的 html 的一部分出现,就在主窗体的下方。
当我直接从浏览器点击此网址“http:///vgn-ext-templating/v/index.jsp?vgnextoid=40289c7c6099f210VgnVCM100000268315acRCRD&vgnextfmt=default”时,recaptcha 和表单按预期显示,但问题仅在错误和转发回时发生到表格。任何帮助表示赞赏。