我之前用过同样的代码,查了查,没有报错。似乎不知何故,这段代码没有做它应该做的事情。我不知道为什么。请帮忙。
ajax 代码似乎不执行 ajax 请求,而是根据表单在 HTML 中设置的内容提交正常的 HTTP get/post 请求。这意味着页面不断刷新。似乎成功函数甚至没有被调用。我已经检查了其余代码是否正在运行,直到$.ajax({刷新页面的行。这可能是我设置表单的方式吗?请注意,我没有提交按钮,因为表单根据按下的按钮向服务器发送不同的操作。
这是HTML代码:
<form id="frmVerify" name="frmVerify" method="post">
<p>Your account has not been verified. You must verify your email to continue logging into your account.</p>
<p>A verification token was sent to your email address, <strong id="username" name="username"><?php echo $_SESSION['username']; ?></strong>. Note that this token expires after 24 hours of issue.</p>
<p>Please check your email to retrieve this token:</p>
<p>
<input type="text" name="token" id="token" />
<button name="action" id="verify" value="<?php echo ACT_VERIFY; ?>">Verify</button>
</p>
<p>You can also: </p>
<p>
<button name="action" id="send" value="<?php echo ACT_SEND_TOKEN; ?>">Send another token</button><br />
<button name="action" id="change" value="<?php echo ACT_CHANGE_EMAIL; ?>">Change Email</button><br />
<button name="action" id="delete" value="<?php echo ACT_DELETE_ACCOUNT; ?>">Delete Account</button>
</p>
<p>
<input type="hidden" name="requester" id="requester" value="Verification" />
</p>
<p><a href="logout.php">Log out</a></p>
</form>
这是JQUERY代码:
function submitForm(action){
var username = $("#username").html();
var token = $("#token").val();
var requester = $("#requester").val();
if(validateLogin()){
$.ajax({
type: "post",
url: "verification.php",
data: "username="+username+"&token="+token+"&action="+action+"&requester="+requester,
success: function(data) {
try{
var jsonObj = $.parseJSON(data);
processOutput(action, jsonObj);
}catch(e){
$("#output").html("PHP module returned non JSON object: <p>"+data+"</p>");
}//try
}//success function
});//ajax
}else{
alert("Please fill UserName & Password!");
return false;
}//if validate
}
$(document).ready(function(){
$("#verify").click(function(){
var action = $(this).val();
submitForm(action);
});
$("#send").click(function(){
var action = $(this).value();
submitForm(action);
});
$("#change").click(function(){
var action = $(this).value();
submitForm(action);
});
$("#delete").click(function(){
var action = $(this).value();
submitForm(action);
});
});