没有从javascript函数调用Ajax jsp页面

时间:2014-04-23 17:53:22

标签: javascript html ajax jsp

这是我的jsp / signup.jsp页面。在这个页面中,我正在调用一个函数onsubmit即
   checkCaptcha1()。我从checkCaptcha1()调用checkCaptcha.jsp页面并传递
    CAPTCHA Code.From checkCaptcha1()函数的参数,它没有调用
     checkCaptcha.jsp页面。     请告诉我我错在哪里。它已经工作了4个小时...........      感谢............................

<script type="text/javascript" src="../javascript/checkCaptcha.js"></script>

<body bgcolor="#B6B6B6">
<form name="signupform" method="post"  onsubmit="return checkCaptcha1()">
<%
  ReCaptcha c = ReCaptchaFactory.newReCaptcha(
              "xxx-", 
            "yyy", false);
  out.print(c.createRecaptchaHtml(null, null));
%>
<!-- CAAPTCHA CODE ENDS -->
<div id="captcha" style="color: red;"></div>
 </form>
</body>


  Here is my checkCaptcha.js function 

  function checkCaptcha1(){
alert("inside checkCaptcha1");
//alert(document.signupform.recaptcha_response_field.value.trim());
var recaptcha_challenge_field =    
    document.signupform.recaptcha_challenge_field.value.trim();
alert(recaptcha_challenge_field);
var recaptcha_response_field = 
  document.signupform.recaptcha_response_field.value.trim();
alert(recaptcha_response_field);
if(recaptcha_response_field.trim()==""){
    document.getElementById("captcha").innerHTML = "Enter CAPTCHA Code";
    return false;
}else{
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return false;
    }
    var url="../ajax/checkCaptcha.jsp";
    url=url+"?recaptcha_challenge_field="+recaptcha_challenge_field+"&"+
  "recaptcha_response_field="+
   recaptcha_response_field;
    alert(url);
    xmlHttp.onreadystatechange=checkCaptcha2;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

}
function checkCaptcha2(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      var showdata = xmlHttp.responseText;
      alert(showdata);
      var str = showdata.split("#$");
      //alert("inside checkCaptcha2 str="+str);
      alert("Str[1]="+str[1]);
      if(str[1]=="fail"){
          alert(str[1]);
          document.getElementById("captcha").innerHTML = "CAPTCHA Validation Failed  
  ! Try Again";
          //flag2=0;
          //alert("Set flag2="+flag2);
          return false;
          //window.location.href("signup.jsp");
      }
      else{
          //alert("Success");
          document.getElementById("captcha").innerHTML = "";
          //flag2 = 1;
          //alert("Set flag2="+flag2);
          //alert("Calling validate2()");
          //validate2();
          return true;
      }
    }
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
}
catch (e)
{
    //Internet Explorer
    try
    {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
}
return xmlHttp;

}

Here is my checkCaptcha.jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="net.tanesha.recaptcha.ReCaptchaImpl"%>
<%@ page import="net.tanesha.recaptcha.ReCaptchaResponse"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      

"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>
 </head>
 <body>
<% System.out.println("Hello check CAPTCHA"); %>
<%  try{
    String remoteAddr = request.getRemoteAddr();
    System.out.println(remoteAddr);
    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
    reCaptcha.setPrivateKey("xxx");

    String challenge = request.getParameter("recaptcha_challenge_field");
    System.out.println(challenge);
    String uresponse = request.getParameter("recaptcha_response_field");
    System.out.println(uresponse);
    ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr,      

 challenge, uresponse);

    if (reCaptchaResponse.isValid()) {
        System.out.print("#$success#$");
        out.print("#$success#$");
    } else {
        System.out.print("#$fail#$");
        out.print("#$fail#$");
    }
}catch(NullPointerException e){
    e.printStackTrace();
}catch(Exception e){
    e.printStackTrace();
}
%>
 </body>
</html>

0 个答案:

没有答案