服务端 ---- 接受请求做出响应
<?php
//账号密码没有输入
if (empty($_POST['username']) || empty($_POST['password'])){
exit('请输入账号或者密码');
}
$username=$_POST['username'];
$password=$_POST['password'];
//账号秘密错误
if ($username=='admin' && $password=='123'){
exit('密码正确');
}
//密码正确
exit('账号或者密码错误');
客户端 ----发送POST请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax发送POST请求</title>
</head>
<body>
<table border="1">
<tr>
<td>用户名</td>
<td><input type="text" id="user"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" id="password"></td>
</tr>
<tr>
<td><button id="btn">提交</button></td>
</tr>
</table>
<script>
// 在用户点击提交的时候将数据提交服务端
var btn=document.getElementById('btn');
btn.onclick=function(){
var xhr1 = new XMLHttpRequest();
xhr1.open('POST','02ajax-POST.php');
var Username= document.getElementById('user').value;
var Password= document.getElementById('password').value;
xhr1.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr1.send(`username=${Username}&password=${Password}`);
xhr1.onreadystatechange=function(){
if (this.readyState==4){
console.log(this.responseText);
}
}
}
</script>
</body>
</html>