需要的文件有:index.html、test.php、test.js、jquery-1.12.4.min.js
index.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="js/test.js"></script>
<meta charset="UTF-8">
<title>ajax</title>
</head>
<body>
<div>
<form id="reg" method="post">
<p class="myerror"></p>
<p>账号:<input type="text" name="user" id="user"></p>
<p>密码:<input type="password" name="pass" id="pass"></p>
<p><input type="submit" name="submit" id="submit" value="提交"></p>
</form>
</div>
</body>
</html>
test.php文件
<?php
$user = $_POST['user'];
$password = $_POST['pass'];
if ($user != 123456 || $password != 12345678) {
echo 'false';
} else {
echo 'success';
}
?>
test.js文件:
$(document).ready(function(){
$('input[type=submit]').click(function(){
$.ajax({
url:'test.php',
type:'POST',
dataType:'json',
data:$('#reg').serialize(),
success:function(){
alert('成功');
},
error:function(){
alert('shibai');
},
});
});
});