如果Ajax返回false,如何阻止表单提交

时间:2014-04-24 12:35:04

标签: javascript ajax jsp

您好我使用旧方式使用Ajax,而不是使用jQuery。如果我的ajax代码返回false,我想停止表单提交。我在提交时调用Ajax函数并返回jsp表单 这是我的代码:

<form name="signupform" method="post"  action="../TenantSignup" onsubmit="return checkCaptcha1()">
</form>

这里是ajax代码:

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();
    alert(XMLHttpRequest);
    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=showData;
    xmlHttp.open("GET",url,false);
    xmlHttp.send(null);
}

 }

 function showData(){
if (xmlHttp.readyState==4 || xmlHttp.readyState==200)
{
    alert("hello");
    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;
    }
}
else{

}
}
function GetXmlHttpObject()
{
alert("XML Object created");
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;
}

这是checkCaptcha.jsp页面:

<%@ 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("6LdlHOsSAAAAACe2WYaGCjU2sc95EZqCI9wLcLXY");

        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>

如果ajax返回false,我不想提交表单..但这里仍然是 如果ajax返回false,则提交表单。我在这里使用同步请求, 即使我发出异步请求也会发生同样的事情。

请帮帮我谢谢。

1 个答案:

答案 0 :(得分:0)

我会强制表单的onsubmit选项每次都返回false并通过ajax提交表单的数据,这样你就可以检查你喜欢的地方,然后提交形成。然后根据需要重定向到另一个页面。它很笨重,但适用于不太复杂的形式。