停止从javascript ajax请求提交

时间:2012-04-15 19:01:25

标签: javascript html ajax submit return

我想停止通过ajax请求的thr结果提交表单... 这是我试图使用的代码: 函数chreg()应该返回一个从ajax请求给出的布尔值,但它没有!

Javascript:

function chreg() {

user = document.getElementById('nome').value;
passw1 = document.getElementById('passw1').value;
passw2 = document.getElementById('passw2').value;
mai = document.getElementById('mail').value;
dat = 'use='+encodeURIComponent(user)+'&pas='+encodeURIComponent(passw1)+'&pas2='+encodeURIComponent(passw2)+'&mail='+encodeURIComponent(mai);

htp = new XMLHttpRequest;
htp.onreadystatechange = function(){

    if (htp.readyState == 4 && htp.status == 200){
        if (htp.responseText == "true"){
            alert('true');
        return true;
        }
        else {
        document.getElementById('er2').innerHTML = '<br/><h3>'+tag_esc(decodeURIComponent(htp.responseText))+'</h3>';
        return false;
        }


    }
};
a = Math.random
  htp.open("POST","checklog.php?a="+a,true);
  htp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    htp.send(dat);   
}

HTML:

<form action="reg.php" method="POST" onsubmit="return chreg(this);">
<table id="tab2"><tr><td>

<label for="user1">Username: <input type="text" name="user" id="nome" /></label></td>      <td>
<label for="mail">Indirizzo e-mail: <input type="text" name="mail" id="mail" /></label>   </td>
</tr>
<tr><td><label for="pass1">Password: <input type="password" name="pass1" id="passw1" />    </label></td>
    <td><label for="pass2">Password (conferma): <input type="password" name="pass2"  id="passw2" /></label></td></tr>
     <tr><td colspan="2"><br /><input type="submit" class="st" value="Registrati" /></td>    </tr>

</table>
<div id="er2"></div>
</form>

但它不起作用! 救命 非常感谢!

3 个答案:

答案 0 :(得分:1)

您的成功处理函数返回true或false,但主chreg方法不返回。

您需要在chreg方法中返回false才能停止表单提交。

    [...]
    htp.send(dat); 
    return false;

由于您正在通过ajax处理表单提交,因此您可能不希望表单在所有情况下进行回发。

答案 1 :(得分:1)

您将无法使其正常工作,因为默认情况下 AJAX是异步 。这意味着您的chreg方法会在请求完成并且其结果已知之前启动请求并立即返回 。这是一个问题,因为您无法在结果知晓之前从chreg返回,因为您需要该结果才能知道要返回的内容

一种解决方案是通过更改第三个参数来使请求同步:

// you will not need to set onreadystatechange at all

htp.open("POST","checklog.php?a="+a,false); // false = synchronous
htp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
htp.send(dat);   
if (htp.responseText == "true"){
    alert('true');
    return true;
}
else {
    document.getElementById('er2').innerHTML = '<br/><h3>'+tag_esc(decodeURIComponent(htp.responseText))+'</h3>';
    return false;
}

这不是最好的解决方案,因为它可能会在请求期间锁定UI,这是不可取的。然而,回到异步模型(没有这个问题)会更加复杂,因为它需要你“向后”做事:

  1. 调用函数chreg并发出AJAX请求; 总是返回false(因此表单未提交)
  2. 当您安装的onreadystatechanged处理程序检测到AJAX已完成时,它会检查响应。
  3. 如果回复在逻辑上为真(表格实际上需要提交),请使用form.submit进行回复。

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<body>

<h1>My First JavaScript</h1>

<p>Please input a number.</p>

<input id="demo" type="text">

<script>
function myFunction()
{
var x=document.getElementById("demo").value;
if(x==""||isNaN(x))
        {
        alert("Not Numeric");
        }
}
</script>