我正在尝试使用ajax表单提交提交表单而不刷新页面。但是,当我第一次单击提交按钮时,它会警告错误(在警报中它显示整个html页面,我有提交表单)。如果我关闭警报并再次点击相同的提交按钮而不刷新页面,它会完美地提交表单。
因此,每次刷新页面时,我都必须点击提交按钮,然后关闭错误提醒,然后再次提交。我很确定它在ajax文件中有一些语法错误。
$('document').ready(function() {
/* validation */
$("#login-form").validate({
rules: {
upass: {
required: true
},
uemail: {
required: true,
email: true
},
},
messages: {
upass: {
required: "please enter your password"
},
uemail: "please enter your email address",
},
errorPlacement: function(error, element) {
$(element).closest('.form-group').find('.help-block').html(error.html());
},
highlight: function(element) {
$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
},
submitHandler: submitForm
});
/* validation */
/* login submit */
function submitForm() {
var data = $("#login-form").serialize();
$.ajax({
type: 'POST',
url: 'login.php',
data: data,
dataType: 'json',
success: function(data) {
// console.log(data);
// alert(JSON.stringify(response));
// if(response == 1){
if (data.status == 'success') {
// alert('Good!');
alert(JSON.stringify(data));
window.location.href = "home.php";
} else {
window.location.href = "login_failed.php";
}
},
error: function(data) {
// console.log(JSON.stringify(data));
alert(JSON.stringify(data));
}
});
return false;
}
});

HTML文件
<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<link rel="stylesheet" href="assets/signup-form.css" type="text/css" />
</head>
<body>
<!--container starts-->
<div class="container">
<!--Header starts-->
<nav class="navbar navbar-fixed-top">
<div class="container-fluid">
<div class="logo">
<a href="index.php"><img src="images/logo.png" style="float:left"/></a>
</div>
<form method="post" action="login.php" id="login-form" class="form-inline" autocomplete="off">
<div class="form-group"><strong style="color:white; font-family:tahoma">Email:</strong>
<input type="email" class="form-control" placeholder="Email address" name="uemail" id="user_email" />
</div>
<!--<input type="email" name="uemail" placeholder="Email" required/>-->
<!--<input type="email" name="u_email" placeholder="Email" required="required"/>-->
<div class="form-group"><strong style="color:white; font-family:tahoma">Password:</strong>
<input type="password" class="form-control" placeholder="Password" name="upass" />
</div>
<!--<input type="password" name="upass" placeholder="Password" required/>-->
<!--<input type="password" name="u_pass" placeholder="********" required="required"/>-->
<button type="submit" name="login" class="btn btn-default">Login</button></br>
<!--<button type="submit" name="login"> Login </button>-->
<a href="forgot_password.php">Forgot your password?</a>
</form>
</div>
</nav>
<script src="js_files/login.js"></script>
<!-- <script src="js_files/login_failed.js"></script> -->
<!-- <script src="register_script.js"></script> -->
<script src="js_files/sign_up.js"></script>
</body>
</html>
这是php文件login.php
<?php
require_once 'class/head.class.php';
if(isset($_POST['login'])) {
$u_email = $_POST['uemail'];
$u_pass = $_POST['upass'];
try {
if(init::loginCheck($u_email) < 3){
if(users::login($u_email, $u_pass)) {
init::loginSuccess($u_email);
// init::redirect('home.php');
$response['status'] = 'success';
// $response['message'] = '<div class="alert alert-danger"><span class="glyphicon glyphicon-ok"></span> registered sucessfully, please confirm your email</div>';
// echo 1;
} else {
init::loginAttempt($u_email);
// init::redirect('home.php');
$response['status'] = 'redirect'; // could not register
// $response['message'] = '<div class="alert alert-warning"><span class="glyphicon glyphicon-info-sign"></span> You have entered incorrect login details. Please try again</div>';
// echo 0;
}
}
else {
$response['status'] = 'redirect';
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
echo json_encode($response);
}
?>
它返回的错误是包含表单的html页面:
{&#34; readyState&#34;:4,&#34; responseText&#34;:&#34; \ n \ n \ n \ n \ n \ n \ nhttp://ajax.googleapis.com/ajax /libs/jquery/3.1.0/jquery.min.js\">\n \ n \ n \ nhttps://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min。 JS \&#34;完整性= \&#34; SHA384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa \&#34; crossorigin = \&#34;匿名\&#34;&GT; \ n \ n \ n \ nhttps://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css \&#34;完整性= \&#34; SHA384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va + PmSTsz / K68vbdEjh4u \&#34; crossorigin = \&#34;匿名\&#34;&GT; \ n \ n \ nhttps://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css \&#34;完整性= \&#34; SHA384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl / SP \&#34; crossorigin = \&#34; anonymous \&#34;&gt; \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n n \ n \ n电子邮件:\ n \ n \ n \ n \ n \ n - &gt; \ n - &gt; \ n \ n密码:\ n \ n \ n \ n \ n \ n - - &gt; \ n - &gt; \ n登录\ n登录&lt; / button&gt; - &gt; \ n \ n忘记密码?\ n \ n \ n \ n \ n \ n \ n \ n&lt; /脚本&GT; - &gt; \ n&lt; / script&gt; - &n; \ n \ n \ n \ n&#34;,&#34; status&#34;:200,&#34; statusText&#34;:&#34; OK&#34;}
答案 0 :(得分:0)
在您的ajax请求中添加此内容
"SessionDate"
这将修复您的错误并添加此错误以查看来自ajax请求的任何错误
async: false,